POJ 1979 Red and Black (简单dfs)
生活随笔
收集整理的這篇文章主要介紹了
POJ 1979 Red and Black (简单dfs)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
簡單dfs,沒什么好說的
代碼:
#include <iostream> using namespace std; typedef long long ll; #define INF 2147483647int w,h; char a[22][22]; int dir[4][2] = {-1,0,1,0,0,-1,0,1}; int ans = 0;void dfs(int x,int y){if(x < 0 || x >= h || y < 0 || y >= w || a[x][y] == '#') return;ans++;a[x][y] = '#';for(int i = 0;i < 4; i++){dfs(x+dir[i][0],y+dir[i][1]);} }int main(){while(cin >> w >> h){if(w == 0 && h == 0) break;ans = 0;int sx,sy;for(int i = 0;i < h; i++){for(int j = 0;j < w; j++){cin >> a[i][j];if(a[i][j] == '@'){sx = i;sy = j;}}}dfs(sx,sy);cout << ans << endl;}return 0; }總結
以上是生活随笔為你收集整理的POJ 1979 Red and Black (简单dfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【算法】差分约束系统
- 下一篇: AOJ 0118: Property D