HDOJ2267(How Many People Can Survive)
How Many People Can Survive
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 116 Accepted Submission(s): 77
Problem Description
Two opposing armies are lost in a big forest. The forest is dangerous ,because it is filled with poisonous gas . People can only survive by staying at the place that is surrounded by trees.As the two armies are opposing, if one army are able to move to the other one , they wil fight with each other. Only the army that with the more people will survive, if the number is equal, no one will survive. Now you are asked to find out how many people will survive.
Input
There are several test cases.
Each case begins with an integer n and m (3<=n,m<=300) stands for the size of forest.
Then follows n lines , each line contains m characters .
‘.’ Stands for empty place
‘#’Stands for tree and can’t been walked through
‘o’Stands for the first army
‘v’stands for the second army
Each case begins with an integer n and m (3<=n,m<=300) stands for the size of forest.
Then follows n lines , each line contains m characters .
‘.’ Stands for empty place
‘#’Stands for tree and can’t been walked through
‘o’Stands for the first army
‘v’stands for the second army
Output
The output contains one line for each data set : two integers, p and q, stands for the number of the first army that can survive and the number of the second army that can survive.
Sample Input
3 3 ..# ovo … 10 10 ...vvvoo.. ########## #....oo.o# #..v.v...# #........# #..####..# #..#o.#..# #..#.v#..# #..####..# ##########
Sample Output
0 0 3 0
//1274798 2009-04-15 20:38:58 Accepted 2267 109MS 424K 1852 B C++ Xredman
#include <iostream>
#include <queue>
using namespace std;
const int N = 302;
typedef struct
{
int x, y;
}Node;
int dir[4][2]= {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int m, n;
char graph[N][N];
bool visited[N][N];
void init()
{
int i, j;
for(i = 0; i < m; i++)
cin>>graph[i];
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
visited[i][j] = false;
}
bool isFinish(int &x, int &y)
{
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
if(graph[i][j] != '#' && visited[i][j] == false)
{
x = i; y = j;
return false;
}
return true;
}
bool isBound(int x, int y)
{
if(x < 0 || y < 0)
return false;
if(x >= m || y >= n)
return false;
return true;
}
void addPeople(int x, int y, int &first, int &second)
{
if(graph[x][y] == 'o')
first++;
if(graph[x][y] == 'v')
second++;
}
void bfs()
{
Node a, b;
int x, y;
bool flag;
int first = 0, second = 0, tfirst, tsecond;
queue<Node> Q;
while(!isFinish(x, y))
{
tfirst = tsecond = 0;
a.x = x; a.y = y;
visited[a.x][a.y] = true;
addPeople(x, y, tfirst, tsecond);
Q.push(a);
flag = false;
////////////////////////////////////////////////
while(!Q.empty())
{
a = Q.front();
Q.pop();
for(int i = 0; i < 4; i++ )
{
b.x = a.x + dir[i][0];
b.y = a.y + dir[i][1];
if(!isBound(b.x, b.y))
flag = true;
else if(graph[b.x][b.y] != '#' && !visited[b.x][b.y])
{
addPeople(b.x, b.y, tfirst, tsecond);
visited[b.x][b.y] = true;
Q.push(b);
}
}
}
if(flag)
continue;
else
{
if(tfirst < tsecond)
second += tsecond;
else if(tfirst > tsecond)
first += tfirst;
}
}
cout<<first<<" "<<second<<endl;
}
int main()
{
while(cin>>m>>n)
{
init();
bfs();
}
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
const int N = 302;
typedef struct
{
int x, y;
}Node;
int dir[4][2]= {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int m, n;
char graph[N][N];
bool visited[N][N];
void init()
{
int i, j;
for(i = 0; i < m; i++)
cin>>graph[i];
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
visited[i][j] = false;
}
bool isFinish(int &x, int &y)
{
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
if(graph[i][j] != '#' && visited[i][j] == false)
{
x = i; y = j;
return false;
}
return true;
}
bool isBound(int x, int y)
{
if(x < 0 || y < 0)
return false;
if(x >= m || y >= n)
return false;
return true;
}
void addPeople(int x, int y, int &first, int &second)
{
if(graph[x][y] == 'o')
first++;
if(graph[x][y] == 'v')
second++;
}
void bfs()
{
Node a, b;
int x, y;
bool flag;
int first = 0, second = 0, tfirst, tsecond;
queue<Node> Q;
while(!isFinish(x, y))
{
tfirst = tsecond = 0;
a.x = x; a.y = y;
visited[a.x][a.y] = true;
addPeople(x, y, tfirst, tsecond);
Q.push(a);
flag = false;
////////////////////////////////////////////////
while(!Q.empty())
{
a = Q.front();
Q.pop();
for(int i = 0; i < 4; i++ )
{
b.x = a.x + dir[i][0];
b.y = a.y + dir[i][1];
if(!isBound(b.x, b.y))
flag = true;
else if(graph[b.x][b.y] != '#' && !visited[b.x][b.y])
{
addPeople(b.x, b.y, tfirst, tsecond);
visited[b.x][b.y] = true;
Q.push(b);
}
}
}
if(flag)
continue;
else
{
if(tfirst < tsecond)
second += tsecond;
else if(tfirst > tsecond)
first += tfirst;
}
}
cout<<first<<" "<<second<<endl;
}
int main()
{
while(cin>>m>>n)
{
init();
bfs();
}
return 0;
}