hdu 4101

比赛的时候先是受以前一个圣神海的题目 用了两遍DFS 第一遍标记出围墙  第二遍求围墙外和每块围墙降为1所需的攻击次数  结果爆栈  改为BFS后AC

DFS的加了一句这个 #pragma comment(linker, "/STACK:1024000000,1024000000")  就解决了爆栈问题

然后就是比赛的时候写了一个bug  竟然过了

//dfs
#include <iostream>
#include <fstream>
#include <cstring>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int dx[5] = {1, -1, 0, 0}, dy[5] = {0, 0, 1, -1};
int g[310][310],sum,n,m;
bool vis[310][310],ok,dd[310][310];
void init()
{
memset(g, 0, sizeof(g));
sum = 0;
ok = false;
memset(vis, 0, sizeof(vis));
memset(dd, 0, sizeof(dd));
}
bool cc(int x, int y)
{
if(x >= 1 && x <= n && y >= 1 && y <= m)
return true;
return false;
}
void dfs(int x, int y)
{
vis[x][y] = 1;
if(x == 1 || x == n || y == 1 || y == m)
ok = true;
for(int i = 0; i < 4; i++)
{
int nx = x+dx[i], ny = y+dy[i];
if(cc(nx, ny) && !vis[nx][ny])
{
if(g[nx][ny] == 0)
{
dfs(nx, ny);
}
else
{
dd[nx][ny] = 1;
}
}
}
}
void dfs2(int x, int y)
{
for(int i = 0; i < 4; i++)
{
int nx = x+dx[i], ny = y+dy[i];
if(cc(nx, ny) && !vis[nx][ny])
{
if(!dd[nx][ny])
{
sum += g[nx][ny];
vis[nx][ny] = 1;
dfs2(nx, ny);
}
else
{
vis[nx][ny] = 1;
sum += g[nx][ny] -1;
}
}
}
}
int main(void)
{
int _x,_y;
while(scanf("%d%d",&n,&m) == 2)
{
init();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
scanf("%d",&g[i][j]);
if(g[i][j] == -1)
_x = i, _y = j;
}
dfs(_x, _y);
if(ok)
{
printf("Ali Win\n");
}
else
{
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++)
{
if(!dd[i][1] && !vis[i][1])
{
vis[i][1] = 1;
sum += g[i][1];
dfs2(i, 1);
}
else if(dd[i][1] && !vis[i][1])
{
vis[i][1] = 1;
sum += g[i][1]-1;
}
if(!dd[i][m] && !vis[i][m])
{
vis[i][m] = 1;
sum += g[i][m];
dfs2(i, m);
}
else if(dd[i][m] && !vis[i][m])
{
vis[i][m] = 1;
sum += g[i][m]-1;
}
}
for(int i = 1; i <= m; i++)
{
if(!dd[1][i] && !vis[1][i])
{
vis[1][i] = 1;
sum += g[1][i];
dfs2(1, i);
}
else if(dd[1][i] && !vis[1][i])
{
vis[1][i] = 1;
sum += g[1][i]-1;
}
if(!dd[n][i] && !vis[n][i])
{
vis[n][i] = 1;
sum += g[n][i];
dfs2(n, i);
}
else if(dd[n][i] && !vis[n][i])
{
vis[n][i] = 1;
sum += g[n][i]-1;
}
}
// printf("%d\n",sum);
if(sum%2)
printf("Ali Win\n");
else
printf("Baba Win\n");
}
}
return 0;
}

#include <iostream>
#include <fstream>
#include <cstring>
#include <queue>
using namespace std;
int dx[5] = {1, -1, 0, 0}, dy[5] = {0, 0, 1, -1};
int g[310][310],sum,n,m;
bool vis[310][310],ok,dd[310][310];
void init()
{
memset(g, 0, sizeof(g));
sum = 0;
ok = false;
memset(vis, 0, sizeof(vis));
memset(dd, 0, sizeof(dd));
}
bool cc(int x, int y)
{
if(x >= 1 && x <= n && y >= 1 && y <= m)
return true;
return false;
}
bool cc2(int x, int y)
{
if(x >= 0 && x <= n+1 && y >= 0 && y <= m+1)
return true;
return false;
}
void dfs(int x, int y)
{
queue<int> Q;
int u = m*x+y;
Q.push(u);
while(!Q.empty())
{
u = Q.front();
Q.pop();
x = (u-1)/m, y = u%m == 0 ? m:u%m;
if(x == 1 || x == n || y ==1 || y == m)
ok = true;
vis[x][y] = 1;
for(int i = 0; i < 4; i++)
{
int nx = x+dx[i], ny = y+dy[i];
if(cc(nx, ny) && !vis[nx][ny])
{
if(g[nx][ny] == 0)
{
int v = nx*m+ny;
Q.push(v);
}
else
{
vis[nx][ny] = 1;
dd[nx][ny] = 1;
}
}
}
}
} void dfs2(int x, int y)
{
vis[x][y] = 1;
queue<int> Q;
int u = (m+2)*x+y;
Q.push(u);
while(!Q.empty())
{
u = Q.front();
Q.pop();
x = u/(m+2), y = u%(m+2);
for(int i = 0; i < 4; i++)
{
int nx = x+dx[i], ny = y+dy[i];
if(cc2(nx, ny) && !vis[nx][ny])
{
if(!dd[nx][ny])
{
sum += g[nx][ny];
vis[nx][ny] = 1;
int v = nx*(m+2)+ny;
Q.push(v);
}
else
{
vis[nx][ny] = 1;
sum += g[nx][ny] -1;
}
}
}
}
}
int main(void)
{
int _x,_y;
while(scanf("%d%d",&n,&m) == 2)
{
init();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
scanf("%d",&g[i][j]);
if(g[i][j] == -1)
_x = i, _y = j;
}
dfs(_x, _y);
if(ok)
{
printf("Ali Win\n");
}
else
{
memset(vis, 0, sizeof(vis));
dfs2(0, 0);
// printf("%d\n",sum);
if(sum%2)
printf("Ali Win\n");
else
printf("Baba Win\n");
}
}
return 0;
}
#include <iostream>
#include <fstream>
#include <cstring>
#include <queue>
using namespace std;
int dx[5] = {1, -1, 0, 0}, dy[5] = {0, 0, 1, -1};
int g[310][310],sum,n,m;
bool vis[310][310],ok,dd[310][310];
void init()
{
memset(g, 0, sizeof(g));
sum = 0;
ok = false;
memset(vis, 0, sizeof(vis));
memset(dd, 0, sizeof(dd));
}
bool cc(int x, int y)
{
if(x >= 1 && x <= n && y >= 1 && y <= m)
return true;
return false;
}
void dfs(int x, int y)
{
queue<int> Q;
int u = m*x+y;
Q.push(u);
while(!Q.empty())
{
u = Q.front();
Q.pop();
x = (u-1)/m, y = u%m == 0 ? m:u%m;
if(x == 1 || x == n || y ==1 || y == m)
ok = true;
vis[x][y] = 1;
for(int i = 0; i < 4; i++)
{
int nx = x+dx[i], ny = y+dy[i];
if(cc(nx, ny) && !vis[nx][ny])
{
if(g[nx][ny] == 0)
{
int v = nx*m+ny;
Q.push(v);
}
else
{
vis[nx][ny] = 1;
dd[nx][ny] = 1;
}
}
}
}
} void dfs2(int x, int y)
{
vis[x][y] = 1;
queue<int> Q;
int u = m*x+y;
Q.push(u);
while(!Q.empty())
{
u = Q.front();
Q.pop();
x = (u-1)/m, y = u%m == 0 ? m:u%m;
for(int i = 0; i < 4; i++)
{
int nx = x+dx[i], ny = y+dy[i];
if(cc(nx, ny) && !vis[nx][ny])
{
if(!dd[nx][ny])
{
sum += g[nx][ny];
vis[nx][ny] = 1;
int v = nx*m+ny;
Q.push(v);
}
else
{
vis[nx][ny] = 1;
sum += g[nx][ny] -1;
}
}
}
}
}
int main(void)
{
int _x,_y;
while(scanf("%d%d",&n,&m) == 2)
{
init();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
scanf("%d",&g[i][j]);
if(g[i][j] == -1)
_x = i, _y = j;
}
dfs(_x, _y);
if(ok)
{
printf("Ali Win\n");
}
else
{
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++)
{
if(!dd[i][1] && !vis[i][1])
{
vis[i][1] = 1;
sum += g[i][1];
dfs2(i, 1);
}
else if(dd[i][1] && !vis[i][1])
{
vis[i][1] = 1;
sum += g[i][1]-1;
}
if(!dd[i][m] && !vis[i][m])
{
vis[i][m] = 1;
sum += g[i][m];
dfs2(i, m);
}
else if(dd[i][m] && !vis[i][m])
{
vis[i][m] = 1;
sum += g[i][m]-1;
}
}
for(int i = 1; i <= m; i++)
{
if(!dd[1][i] && !vis[1][i])
{
vis[1][i] = 1;
sum += g[1][i];
dfs2(1, i);
}
else if(dd[1][i] && !vis[1][i])
{
vis[1][i] = 1;
sum += g[1][i]-1;
}
if(!dd[n][i] && !vis[n][i])
{
vis[n][i] = 1;
sum += g[n][i];
dfs2(n, i);
}
else if(dd[n][i] && !vis[n][i])
{
vis[n][i] = 1;
sum += g[n][i]-1;
}
}
// printf("%d\n",sum);
if(sum%2)
printf("Ali Win\n");
else
printf("Baba Win\n");
}
}
return 0;
}
上一篇:Python和Python解释器


下一篇:QT 对话框二