标签:sicily
Time Limit: 1 secs, Memory Limit: 292.96875 MB
It‘s Bessie‘s feeding time, and Farmer John is trying to decide where to put her. FJ has a farm that comprises W x H (1 <= W <= 750; 1 <= H <= 750) squares and is partitioned into
one or more separate pastures by rocks both large and small. Every pasture contains some grass and some rocks.
Bessie is a hungry little cow and just loves to eat, eat, eat her grass. She can move from any square to any other square that is horizontally, vertically, or diagonally adjacent. Bessie can‘t cross the rocks because they hurt her feet, and, of course, she
can‘t leave the farm. Bessie wants to know the maximum number of squares of grass that she can eat.
FJ has a map of his farm, where a ‘.‘ represents a square of grass, and a ‘*‘ represents a rock. Consider this 10x8 map and a detailed breakdown of the extent of each of its three pastures:
...*....** | 111*....** ...*2222** ...*....**
..**....** | 11**....** ..**2222** ..**....**
...*....** | 111*....** ...*2222** ...*....**
...**.*.** | 111**.*.** ...**2*2** ...**.*.**
***.**.*** | ***1**.*** ***.**2*** ***.**.***
...**.*.** | 111**.*.** ...**2*2** ...**.*.**
...*.***** | 111*.***** ...*2***** ...*.*****
...***..** | 111***..** ...***..** ...***33**
* Line 1: Two space-separated integers: W and H
* Lines 2..H+1: Line i+1 describes field row i with W characters (and no spaces), each either ‘.‘ or ‘*‘
* Line 1: A single integer that represents the maximum number of squares of grass that Bessie can eat.
10 8 ...*....** ..**....** ...*....** ...**.*.** ***.**.*** ...**.*.** ...*.***** ...***..**
21
2010中山大学新手赛-网络预选赛
队列dfs
#include <queue> #include <iostream> #include <stdio.h> using namespace std; #define MAX 755 bool map[MAX][MAX];//储存地图 bool vis[MAX][MAX];//储存是否已经访问过,注意刚开始默认是false的,上同 int w, h; struct point { int ii, jj; }; int dfs(int start_i, int start_j) { queue<point> q;//用队列来代替深搜,否则爆栈 point temp_p, temp_p_last; temp_p.ii = start_i; temp_p.jj = start_j; vis[start_i][start_j] = true; q.push(temp_p); int counter = 0; while (!q.empty()) { temp_p = q.front(); q.pop(); map[temp_p.ii][temp_p.jj] = false; counter++; //以下是八个方向的搜索 if (temp_p.ii > 0 && map[temp_p.ii - 1][temp_p.jj] && !vis[temp_p.ii - 1][temp_p.jj]) { temp_p_last.ii = temp_p.ii - 1; temp_p_last.jj = temp_p.jj; q.push(temp_p_last); vis[temp_p.ii - 1][temp_p.jj] = true; } if (temp_p.jj > 0 && map[temp_p.ii][temp_p.jj - 1] && !vis[temp_p.ii][temp_p.jj - 1]) { temp_p_last.ii = temp_p.ii; temp_p_last.jj = temp_p.jj - 1; q.push(temp_p_last); vis[temp_p.ii][temp_p.jj - 1] = true; } if (temp_p.ii < h - 1 && map[temp_p.ii + 1][temp_p.jj] && !vis[temp_p.ii + 1][temp_p.jj]) { temp_p_last.ii = temp_p.ii + 1; temp_p_last.jj = temp_p.jj; q.push(temp_p_last); vis[temp_p.ii + 1][temp_p.jj] = true; } if (temp_p.jj < w - 1 && map[temp_p.ii][temp_p.jj + 1] && !vis[temp_p.ii][temp_p.jj + 1]) { temp_p_last.ii = temp_p.ii; temp_p_last.jj = temp_p.jj + 1; q.push(temp_p_last); vis[temp_p.ii][temp_p.jj + 1] = true; } if (temp_p.ii > 0 && temp_p.jj > 0 && map[temp_p.ii - 1][temp_p.jj - 1] && !vis[temp_p.ii - 1][temp_p.jj - 1]) { temp_p_last.ii = temp_p.ii - 1; temp_p_last.jj = temp_p.jj - 1; q.push(temp_p_last); vis[temp_p.ii - 1][temp_p.jj - 1] = true; } if (temp_p.ii > 0 && temp_p.jj < w - 1 && map[temp_p.ii - 1][temp_p.jj + 1] && !vis[temp_p.ii - 1][temp_p.jj + 1]) { temp_p_last.ii = temp_p.ii - 1; temp_p_last.jj = temp_p.jj + 1; q.push(temp_p_last); vis[temp_p.ii - 1][temp_p.jj + 1] = true; } if (temp_p.ii < h - 1 && temp_p.jj < w - 1 && map[temp_p.ii + 1][temp_p.jj + 1] && !vis[temp_p.ii + 1][temp_p.jj + 1]) { temp_p_last.ii = temp_p.ii + 1; temp_p_last.jj = temp_p.jj + 1; q.push(temp_p_last); vis[temp_p.ii + 1][temp_p.jj + 1] = true; } if (temp_p.ii < h - 1 && temp_p.jj > 0 && map[temp_p.ii + 1][temp_p.jj - 1] && !vis[temp_p.ii + 1][temp_p.jj - 1]) { temp_p_last.ii = temp_p.ii + 1; temp_p_last.jj = temp_p.jj - 1; q.push(temp_p_last); vis[temp_p.ii + 1][temp_p.jj - 1] = true; } } return counter; } int main() { int i, j, max, temp_num; char temp[755]; scanf("%d%d\n", &w, &h); for (i = 0; i < h; i++) { gets(temp); for (j = 0; j < w; j++) { if (temp[j] == '.') { map[i][j] = true; } } } for (i = 0, max = 0; i < h; i++) { for (j = 0; j < w; j++) { if (map[i][j]) { temp_num = dfs(i, j); max = temp_num > max ? temp_num : max; } } } printf("%d\n", max); return 0; }
标签:sicily
原文地址:http://blog.csdn.net/u012925008/article/details/44739137