Untitled
unknown
plain_text
a year ago
1.6 kB
3
Indexable
Never
#include <bits/stdc++.h> using namespace std; ifstream fin("cutu.in"); ofstream fout("cutu.out"); int n, m, x, y, cx, cy, a[1005][1005], k=1; queue<pair<int, int> > q; pair<int, int> p; char v[1005][1005], b[10005]; void solve(){ fin>>n>>m; for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ fin>>v[i][j]; if(v[i][j]=='_'){ a[i][j]=0; } else if(v[i][j]=='C'){ x=i, y=j, a[i][j]=0; } else if(v[i][j]=='M'){ a[i][j]=0, cx=i, cy=j; } else{ a[i][j]=-1; } } } a[x][y]=1; q.push({x, y}); while(!q.empty()){ int cxi=q.front().first; int cyi=q.front().second; if(cxi-1>=1 && a[cxi-1][cyi]==0){ a[cxi-1][cyi]=a[cxi][cyi]+1; q.push({cxi-1, cyi}); } if(cyi-1>=1 && a[cxi][cyi-1]==0){ a[cxi][cyi-1]=a[cxi][cyi]+1; q.push({cxi, cyi-1}); } if(cyi+1<=m && a[cxi][cyi+1]==0){ a[cxi][cyi+1]=a[cxi][cyi]+1; q.push({cxi, cyi+1}); } if(cxi+1<=n && a[cxi+1][cyi]==0){ a[cxi+1][cyi]=a[cxi][cyi]+1; q.push({cxi+1, cyi}); } q.pop(); } if(a[cx][cy]==-1){ fout<<0; } else{ fout<<a[cx][cy]-1; } } int main(){ solve(); return 0; }