标签:
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 26181 | Accepted: 14213 |
Description
Input
Output
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
45 59 6 13
题意为:求@可以到达.(点)的最多个数, 别忘了@本身也算一个哦~!#代表不可以穿越!
大体就是这么个意思,不多说,直接上代码.......
#include <iostream> #include <stdio.h> #include <queue> #include <string.h> #define N 30 using namespace std; int ans = 0, m, n;//ans用于计数的! int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};//一点小技巧 int vis[N][N], map[N][N];//vis数组是用于标记是否访问过~ void bfs(int x, int y) { int i,ty,tx,ttx,tty; queue<pair<int, int> > Q;//不会用queue的可以去百度看看 很好用滴 Q.push(make_pair(x, y)); vis[x][y] = 1; while(!Q.empty()) { tx = Q.front().first, ty = Q.front().second; Q.pop();//别忘了一直要删除队头 for(i = 0; i < 4; i++) { ttx = tx + dx[i], tty = ty + dy[i]; if(ttx >= 0 && ttx < m && tty >= 0 && tty < n) if(!vis[ttx][tty] && !map[ttx][tty]) { vis[ttx][tty] = 1; ans++; Q.push(make_pair(ttx, tty)); } } } } int main() { int i, j, x, y; while(~scanf("%d%d", &n, &m)) { ans = 1; memset(vis, 0, sizeof(vis));//比for循环清零好使 getchar();//有些人输入老是出问题,看看是不是这的原因 if(n == 0 && m == 0) break; char s[N]; for(i = 0; i < m; i++) { gets(s); for(j = 0; j < n; j++) { if(s[j] == ‘.‘) map[i][j] = 0; else if(s[j] == ‘#‘) map[i][j] = 1; else x = i, y = j;//@的坐标 } } bfs(x, y); printf("%d\n", ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/yu0111/p/4727675.html