The Basic Search is finished with a simple BFs.
Give a grass, and then take the grass as the center of the Nine Palaces, spread to eight directions around each week, and ask how many weeks can fill the farm.
Traverse the entire map. The week number of the last outgoing queue is required.
1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 using namespace std; 7 8 struct Point 9 {10 int x, y;11 int days;12 }start;13 14 char map[105][105];15 int r, c, ans;16 int dir[8][2] = {1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1};17 18 bool islegal(int x, int y)19 {20 return (x>=0 && x<r && y>=0 && y<c &&map[x][y]==‘.‘);21 }22 23 void BFS(void)24 {25 queue<Point> qu;26 Point cur, next;27 start.days = 0;28 qu.push(start);29 while(!qu.empty())30 {31 cur = qu.front();32 qu.pop();33 for(int i = 0; i < 8; ++i)34 {35 next = cur;36 next.x += dir[i][0], next.y += dir[i][1];37 if(islegal(next.x, next.y))38 {39 next.days = ans = cur.days + 1;40 map[next.x][next.y] = ‘*‘;41 qu.push(next);42 }43 }44 }45 }46 47 int main(void)48 {49 #ifdef LOCAL50 freopen("1030in.txt", "r", stdin);51 #endif52 53 scanf("%d%d%d%d", &c, &r, &start.y, &start.x);54 --start.x, --start.y; 55 for(int i = r - 1; i >= 0; --i)56 scanf("%s", map[i]);57 map[start.x][start.y] = ‘*‘;58 ans = 0;59 BFS();60 printf("%d\n", ans);61 return 0;62 }
Code Jun