queuetu
#include <iostream> using namespace std; int n, m; struct position{ int x; int y; }; int dx[4] = {0,0,-1,1}; int dy[4] = {-1,1,0,0}; position start, cellempty; int map[3003][3003]; int visit[3003][3003]; position queue[1000000]; int queuesize; int queuefont; int countcell; int max(int xx, int yy) { return xx > yy ? xx : yy; } int main() { //freopen("input.txt","r",stdin); int ntc; cin >> ntc; for (int tc=1; tc<=ntc; tc++) { cin >> n >> m; for (int i = 0; i <=n+1; i++) for (int j =0; j<=m+1; j++) { visit[i][j] = 0; map[i][j] = 0; } countcell = 0; cin >> start.x >> start.y; for (int i = 1; i <=n; i++) for (int j =1; j<=m; j++) { cin >> map[i][j]; if (map[i][j] == 1) countcell++; if (map[i][j] == 2) { cellempty.x = i; cellempty.y = j; } } //cout << tc << " co " << countcell << endl; //cout << start.x << " "<< start.y << endl; int maxtime = 0; queuesize = 0; queuefont = 0; position tmp; tmp.x = start.x; tmp.y = start.y; visit[tmp.x][tmp.y] = 1; queue[++queuesize] = tmp; while (queuefont < queuesize) { position current = queue[++queuefont]; //cout << current.x << " " << current.y <<endl; for (int k = 0; k < 4; k++) { int uu = current.x + dx[k]; int vv = current.y + dy[k]; if (map[uu][vv] == 1 && visit[uu][vv] == 0) { //cout << uu << " " << vv << endl; countcell--; position tmp1; tmp1.x = uu; tmp1.y = vv; visit[uu][vv] = visit[current.x][current.y] + 1; maxtime = max(maxtime, visit[uu][vv]); queue[++queuesize] = tmp1; } } } //cout << countcell << endl; int count1 = -1; int count2 = countcell == 1 ? maxtime : -1; for (int k = 0; k < 4; k++) { int uu = cellempty.x + dx[k]; int vv = cellempty.y + dy[k]; if (visit[uu][vv] == 0) { count1 = -1; break; } count1 = max(count1, visit[uu][vv]); } cout <<"Case #"<<tc<< endl<< count1 << " " << count2 << endl; } return 0; }
Leave a Comment