| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
50题,光棍留念,哈哈#include <iostream>
#include <string>
using namespace std;
const int N = 25;
char grid[N][N];
bool visit[N][N];
void dfs(int height, int width, int x, int y, int &per);
int main()
{
int height = 0, width = 0, x = 0, y = 0;
while (1)
{
int perimeter = 0;
cin >> height >> width >> x >> y;
if (height + width + x + y == 0) break;
memset(visit, false, sizeof(visit));
for (int i=1; i<=height; i++)
{
for (int j=1; j<=width; j++) cin >> grid[i][j];
}
for (int i=0; i<=width+1; i++)
{
grid[0][i] = '.';
grid[height+1][i] = '.';
}
for (int i=0; i<=height+1; i++)
{
grid[i][0] = '.';
grid[i][width+1] = '.';
}
dfs(height, width, x, y, perimeter);
cout << perimeter << endl;
}
return 0;
}
void dfs(int height, int width, int x, int y, int &per)
{
visit[x][y] = true;
if (grid[x-1][y-1] == 'X' && visit[x-1][y-1] == false)
{
dfs(height, width, x-1, y-1, per);
}
if (grid[x-1][y+1] == 'X' && visit[x-1][y+1] == false)
{
dfs(height, width, x-1, y+1, per);
}
if (grid[x+1][y-1] == 'X' && visit[x+1][y-1] == false)
{
dfs(height, width, x+1, y-1, per);
}
if (grid[x+1][y+1] == 'X' && visit[x+1][y+1] == false)
{
dfs(height, width, x+1, y+1, per);
}
if (grid[x-1][y] == 'X')
{
if (visit[x-1][y] == false) dfs(height, width, x-1, y, per);
}
else per++;
if (grid[x][y-1] == 'X')
{
if (visit[x][y-1] == false) dfs(height, width, x, y-1, per);
}
else per++;
if (grid[x][y+1] == 'X')
{
if (visit[x][y+1] == false) dfs(height, width, x, y+1, per);
}
else per++;
if (grid[x+1][y] == 'X')
{
if (visit[x+1][y] == false)dfs(height, width, x+1, y, per);
}
else per++;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator