Created
October 17, 2012 06:05
-
-
Save logicmd/3903933 to your computer and use it in GitHub Desktop.
1979:Red and Black http://poj.grids.cn/practice/1979
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************************* | |
Author: logicmd | |
Created Time: 2012/10/17 13:16:31 | |
File Name: RedAndBlack.cpp | |
Description: | |
************************************************************************/ | |
#include <cassert> | |
#include <iostream> | |
#include <vector> | |
#include <map> | |
#include <cstdio> | |
#include <string> | |
#include <utility> | |
#include <algorithm> | |
#define MAX_SIZE 20 | |
using namespace std; | |
int maze[MAX_SIZE][MAX_SIZE]; | |
int W, H; | |
//int direction[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; | |
int dfs(int i, int j) | |
{ | |
if( i<0 || i>=H || j<0 || j>=W ) | |
return 0; | |
if( maze[i][j] == -1 ) | |
return 0; | |
maze[i][j] = -1; | |
return(1+dfs(i+1,j)+dfs(i-1,j)+dfs(i,j+1)+dfs(i,j-1)); | |
} | |
int main() | |
{ | |
vector<int> re; | |
while(true) | |
{ | |
cin >> W >> H; | |
if(W==0&&H==0) | |
break; | |
char c; | |
int si, sj; | |
for(int i=0; i<H; i++) | |
{ | |
for(int j=0; j<W; j++) | |
{ | |
cin >> c; | |
if(c == '.') | |
maze[i][j]=0; | |
else if(c == '#') | |
maze[i][j]=-1; | |
else if(c == '@') | |
{ | |
si = i; | |
sj = j; | |
maze[i][j] = 0; | |
} | |
} | |
} | |
int sum = dfs(si, sj); | |
re.push_back(sum); | |
} | |
for(vector<int>::iterator its = re.begin(); its != re.end(); its++) | |
{ | |
cout << *(its) << endl; | |
} | |
//system("PAUSE"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment