/******************************************************* * Copyright (C) 2015 Haotian Wu * * This file is the solution to the question: * https://www.hackerrank.com/challenges/count-luck * * Redistribution and use in source and binary forms are permitted. *******************************************************/ #include #include #include #include #include #include #include #include #include using namespace std; // We can first BFS to find the exit, then trace back to get the path, finally count the time "magic" is needed. // Just watch out you shouldn't check if there's a branch at the '*' point. struct point { int x; int y; }; int deltax[] = {0, 0, 1, -1}; int deltay[] = {1, -1, 0, 0}; int main() { int tt; scanf("%d",&tt); while (tt--) { int m,n,k; char mp[101][101]; scanf("%d %d",&n,&m); for (int i=0;i= 0 && x+deltax[dir] < n && y+deltay[dir] >= 0 && y+deltay[dir] < m && visited[x+deltax[dir]][y+deltay[dir]] == 0 && mp[x+deltax[dir]][y+deltay[dir]] != 'X') { q[ed++] = (point){x+deltax[dir],y+deltay[dir]}; pre[x+deltax[dir]][y+deltay[dir]] = (point){x,y}; visited[x+deltax[dir]][y+deltay[dir]] = 1; } be ++; } vector path; point curpoint = q[be]; curpoint = pre[curpoint.x][curpoint.y]; while (curpoint.x != -1) //Push all but the '*' point on the path into the vector. { path.push_back(curpoint); curpoint = pre[curpoint.x][curpoint.y]; } int len = path.size(); memset(visited,0,sizeof(visited)); for (int i=len-1;i>=0;i--) { int cnt = 0; int x = path[i].x, y = path[i].y; visited[x][y] = 1; for (int dir = 0; dir < 4; dir ++) if (x+deltax[dir] >= 0 && x+deltax[dir] < n && y+deltay[dir] >= 0 && y+deltay[dir] < m && visited[x+deltax[dir]][y+deltay[dir]] == 0 && mp[x+deltax[dir]][y+deltay[dir]] != 'X') cnt++; if (cnt >= 2) k--; } if (k==0) printf("Impressed\n"); else printf("Oops!\n"); } }