poj Red and Black

原创 2015年11月21日 18:25:05

题目链接http://poj.org/problem?id=1979

一个最基础的dfs搜索题

//#include <bits/stdc++.h>
#include "iostream"
#include "cstring"
using namespace std;

typedef long long ll;

int k[25][25];//标记数组
char c[25][25];
int ans = 0; //可走的步数

void dfs(int m, int n,int i,int j,char c[25][25]) {

    if(c[i][j] == '#') return;
    if(!k[i][j]) {
        ans++;
        k[i][j] = 1;
    }
    //递归搜索
    if(i != 0 && !k[i-1][j]) dfs(m,n,i-1,j,c);
    if(i != m-1 && !k[i+1][j]) dfs(m,n,i+1,j,c);
    if(j != 0 && !k[i][j-1]) dfs(m,n,i,j-1,c);
    if(j != n-1 && !k[i][j+1]) dfs(m,n,i,j+1,c);
}


int main(){

    ios::sync_with_stdio(false);

    int m,n,x,y;
    while(cin >> m >> n && (m ||n)) {

         memset(k,0,sizeof(k));
         for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++) {
                cin >> c[i][j];
                if(c[i][j] == '@'){
                    x = i;
                    y = j;
                }
         }
         dfs(n,m,x,y,c);
         cout << ans << endl;
         ans = 0;

    }


    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

递归回溯法实战(一)——(Red and Black)红黑砖迷阵(POJ1979)

问题模型有一个矩形的房间里铺满正方形瓷砖。每块瓷砖涂成黑色或红色。一个人站在黑色的瓷砖上,从此出发,可以移动到四个相邻的瓷砖之一,但只能移动到黑色的瓷砖上。计算他通过重复上述移动所能经过的黑砖数。 ...

poj1979 Red and Black

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24297   ...

POJ 1979-Red and Black【基础DFS】

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

poj 1979 Red and Black(深搜)

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23390   ...

POJ 1979-Red and Black [dfs] 《挑战程序设计竞赛》2.1

Description There is a rectangular room, covered with square tiles. Each tile is colored either re...

POJ_1979_Red and Black

POJ 1979 There is a rectangular room, covered with square tiles. Each tile is colored either red or ...

POJ 1979 Red and Black (DFS的简单应用)

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21765   ...

poj1979 Red and Black(DFS)

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25797   ...

Poj 1979 Red and Black

Language: DefaultEspañol Red and Black Time Limit: 1000MS   Memory Limit: 30000K ...

POJ1979——Red and Black

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 31223   ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)