本文主要是介绍HDU1312 Red and Black,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
大致题意:搜索邻接字符到底有多少个
#define LOCAL#include <iostream>
#include <fstream>using namespace std;const int maxn = 20 +1;
char maze[maxn][maxn];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sum;
int W, H, sx, sy;
void dfs(int x, int y)
{maze[x][y] = '#';for (int i = 0; i < 4; ++i) {int nx = x + dx[i];int ny = y + dy[i];if (0 <= nx && nx < H && 0 <= ny && ny < W &&maze[nx][ny] == '.') {dfs(nx, ny);sum++;}}
}int main()
{
#ifdef LOCALfreopen("input.txt", "r", stdin);
#endif// Input OKwhile (cin >> W >> H && W && H) {for (int i = 0; i < H; ++i) {for (int j = 0; j < W; ++j) {cin >> maze[i][j];if (maze[i][j] == '@') {sx = i; sy = j;}}}sum = 1;dfs(sx, sy);cout << sum << endl;}return 0;
}
当然,不能简单贴代码,这题非常的经典,是一个经典的DFS矩阵的框架。再多也没什么说的了,就这样
这篇关于HDU1312 Red and Black的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!