Test instructions: Within a n*n matrix, two people want to find a "treasure", denoted by-1. There are open space, each round can be arbitrarily shuttle, with 0, there are monsters, can not directly through, need to kill, cut 1 HP per turn, equal to the death of 0 o'clock, by two people from the periphery, each one round action, ask who can get "treasure."
Puzzle: Seemingly game, the same can be considered from the winning state, when the treasure around only the last lap of blood 1 of the monster, who first killed the monster, who lost.
Two people do not want to kill the treasure door of the first strange, so the two people's State is always chopping the door outside of the strange, you knife me a knife. So all we need to know is that apart from the last lap, the sum of the monsters ' blood outside the door can be judged by the odd or even number of the first or the latter to win.
Start from the beginning of the wide search, find the most inner circle of monsters, mark it as 1
Again from the periphery to search, search for peripheral monsters, num + = Monster Blood volume. I found the inner circle. num + = (Monster Blood-1), leave a strange janitor
Code:
#include <stdio.h>
#include <iostream>
#include <queue>
using namespace Std;
int mark_bfs[333][333], mark_bfs2[333][333], map[333][333], flag, NUM, n, m;
int dx[4] = {1,-1, 0, 0};
int dy[4] = {0, 0, 1,-1};
struct Node
{
int x, y;
}st;
void BFs (Node st)
{
Queue<node> p;
P.push (ST);
MARK_BFS[ST.X][ST.Y] = 1;
Node V, vn;
while (!p.empty ())
{
VN = P.front ();
P.pop ();
for (int i =0; i < 4; i++)
{
v.x = vn.x + dx[i];
V.Y = Vn.y + dy[i];
if (v.x = = N-1 | | v.x = = 0 | | v.y = = M-1 | | v.y = 0) && (map[v.x][v.y] = = 0)) {flag = 1; return;}
if (Mark_bfs[v.x][v.y]) continue;
if (map[v.x][v.y] = = 0)
P.push (v);
MARK_BFS[V.X][V.Y] = 1;
}
}
}
void Bfs2 (Node st)
{
Queue<node> p;
P.push (ST);
if (Mark_bfs2[st.x][st.y]) return;
MARK_BFS2[ST.X][ST.Y] = 1;
Node V, vn;
while (!p.empty ())
{
VN = P.front ();
P.pop ();
num + = MAP[VN.X][VN.Y];
for (int i = 0; i < 4; i++)
{
v.x = vn.x + dx[i];
V.Y = Vn.y + dy[i];
if (v.x >= n | | v.x < 0 | | v.y >= m | | v.y < 0) continue;
if (Mark_bfs2[v.x][v.y]) continue;
if (MARK_BFS[V.X][V.Y]) {num + = (Map[v.x][v.y]-1); MARK_BFS2[V.X][V.Y] = 1; Continue }
MARK_BFS2[V.X][V.Y] = 1;
P.push (v);
}
}
}
int main ()
{
while (scanf ("%d%d", &n, &m)!=eof)
{
memset (MARK_BFS, 0, sizeof (MARK_BFS));
memset (MARK_BFS2, 0, sizeof (MARK_BFS2));
for (int i = 0; i < n; i + +)
{
for (int j = 0; J < m; J + +)
{
scanf ("%d", &map[i][j]);
if (map[i][j] = =-1)
{
St.x = i;
St.y = j;
}
}
}
Flag = 0;
num = 0;
BFS (ST);
if (flag) {printf ("Ali win\n"); continue;}
for (int i = 0; i < m; i++)
{
st.x = 0;
St.y = i;
BFS2 (ST);
St.x = n-1;
St.y = i;
BFS2 (ST);
}
for (int i = 0; i < n; i++)
{
St.x = i;
St.y = 0;
BFS2 (ST);
St.x = i;
St.y = m-1;
BFS2 (ST);
}
if (num% 2) printf ("Ali win\n");
else printf ("Baba win\n");
}
}
HDU 4101 interesting wide-search