POJ 1562 解题报告

来源:互联网 发布:李毅吧吧主是谁 知乎 编辑:程序博客网 时间:2024/06/05 18:51

这道题是简单题。就是对每个有油的点跑DFS,把相邻的有油点都标记为没油。这称为一趟。输出趟数即可。

thestoryofsnow1562Accepted172K0MSC++1318B

/* ID: thestor1 LANG: C++ TASK: poj1562 */#include <iostream>#include <fstream>#include <cmath>#include <cstdio>#include <cstring>#include <limits>#include <string>#include <vector>#include <list>#include <set>#include <map>#include <queue>#include <stack>#include <algorithm>#include <cassert>using namespace std;const int MAXM = 100 + 1;const int MAXN = 100 + 1;int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};void dfs(char grid[][MAXN], int r, int c, const int R, const int C){grid[r][c] = '*';for (int d = 0; d < 8; ++d){int nr = r + dx[d];int nc = c + dy[d];if (nr >= 0 && nr < R && nc >= 0 && nc < C && grid[nr][nc] == '@'){dfs(grid, nr, nc, R, C);}}}int main(){char grid[MAXM][MAXN];int m, n;while (scanf("%d%d", &m, &n) > 0 && m){for (int i = 0; i < m; ++i){scanf("%s", grid[i]);}// printf("[debug]grid:\n");// for (int i = 0; i < m; ++i)// {// printf("[");// for (int j = 0; j < n; ++j)// {// printf("%c", grid[i][j]);// }// printf("]\n");// }int cnt = 0; for (int i = 0; i < m; ++i){for (int j = 0; j < n; ++j){if (grid[i][j] == '@'){cnt++;dfs(grid, i, j, m, n);}}}printf("%d\n", cnt);}return 0;}


0 0
原创粉丝点击