Skip to content

Instantly share code, notes, and snippets.

@logicmd
Created October 17, 2012 06:05
Show Gist options
  • Save logicmd/3903933 to your computer and use it in GitHub Desktop.
Save logicmd/3903933 to your computer and use it in GitHub Desktop.
/*************************************************************************
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