|
楼主 |
发表于 2005-7-21 09:13
|
显示全部楼层
#include <iostream>
#include <queue>
#include <fstream>
using namespace std;
class point
{
public :
int m_x;
int m_y;
point(int x, int y)
{
m_x = x;
m_y = y;
};
};
int main()
{
//ifstream cin("in.txt");
//ofstream cout("out.txt");
char map[22][22];
bool visited[22][22];
int row,col,x,y,perimeter;
queue<point> myqueue;
while(1)
{
cin >> row >> col >> x >> y;
if(row == 0 && col == 0)
{
break;
}
else
{
perimeter = 0;
int i,j;
for(i = 1 ;i <= row;i++ )
{
for(j = 1;j <= col;j++)
{
cin >> map[j];
}
}
for(i = 0 ;i < 22;i++ )
{
for(j = 0;j < 22;j++)
{
visited[j]= false;
}
}
point temp(x,y);
myqueue.push(temp);
visited[x][y] = true;
while(!myqueue.empty())
{
//four direction
if(x + 1 > row || map[x+1][y] =='.')
{
perimeter++;
}
else
{
if(x+1 <= row && !visited[x+1][y] && map[x+1][y] == 'X')
{
point temp2(x+1,y);
myqueue.push(temp2);
visited[x+1][y] = true;
}
}
if(x - 1 <= 0 || map[x-1][y] == '.')
{
perimeter++;
}
else
{
if(x - 1 >=1 && !visited[x-1][y] && map[x-1][y] == 'X')
{
point temp3(x-1,y);
myqueue.push(temp3);
visited[x-1][y] = true;
}
}
if(y + 1 > col || map[x][y+1] == '.')
{
perimeter++;
}
else
{
if(y+1 <=col && !visited[x][y+1] && map[x][y+1] == 'X')
{
point temp4(x,y+1);
myqueue.push(temp4);
visited[x][y+1] = true;
}
}
if(y - 1 <= 0 || map[x][y-1] == '.')
{
perimeter++;
}
else
{
if(y - 1 >=1 && !visited[x][y-1] && map[x][y-1] == 'X')
{
point temp5(x,y-1);
myqueue.push(temp5);
visited[x][y-1] = true;
}
}
//fout corners
if(x + 1 <= row && y + 1 <= col)
{
if( !visited[x+1][y+1] && map[x+1][y+1] == 'X')
{
point temp6(x+1,y+1);
myqueue.push(temp6);
visited[x+1][y+1] = true;
}
}
if(x + 1 <= row && y - 1 >= 1)
{
if( !visited[x+1][y-1] && map[x+1][y-1] == 'X')
{
point temp7(x+1,y-1);
myqueue.push(temp7);
visited[x+1][y-1] = true;
}
}
if(x - 1 >= 1 && y + 1 <= col)
{
if( !visited[x-1][y+1] && map[x-1][y+1] == 'X')
{
point temp8(x-1,y+1);
myqueue.push(temp8);
visited[x-1][y+1] = true;
}
}
if(x - 1 >= 1 && y - 1 >= 1)
{
if( !visited[x-1][y-1] && map[x-1][y-1] == 'X')
{
point temp9(x-1,y-1);
myqueue.push(temp9);
visited[x-1][y-1] = true;
}
}
if( !myqueue.empty())
{
myqueue.pop();
}
if( !myqueue.empty())
{
x = myqueue.front().m_x;
y = myqueue.front().m_y;
}
}
cout << perimeter<<endl;
}
}
} |
|