您的位置:首页 > 其它

ZOJ 2411 Link Link Look(BFS)

2013-10-02 09:54 375 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1411

题目大意:连连看,给出每次连线的两个坐标,求能消去多少方块,拐弯最多2次

Sample Input

3 4
1 1 2 2
3 3 4 4
2 2 1 1
6
1 1 1 2
1 3 1 4
2 1 2 2
2 3 2 4
3 1 3 2
3 3 3 4

0 0

Sample Output

12

分析:连线可以从外围绕过去,用BFS求出两个坐标能够到达的最少拐弯次数。注意是最少拐弯次数,而不是最短距离

这道题目坑死我了,倒不是因为它的算法难,是一些小知识点

代码如下:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<queue>
using namespace std;

int n,m;
int map[105][105];
bool vis[105][105];
int dx[]= {-1,0,0,1};
int dy[]= {0,-1,1,0};
struct node
{
int x,y,turn;
} st;
queue<node>q;

bool BFS(int x1,int y1,int x2,int y2)
{
if(map[x1][y1]==0 || map[x2][y2]==0 ||map[x1][y1]!=map[x2][y2] ||x1==x2&&y1==y2)
return false;
st.x = x1;
st.y = y1;
while(!q.empty()) q.pop();  //坑死,while写成了if,半天没看出来
memset(vis,0,sizeof(vis));
st.turn = -1;
q.push(st);
while(!q.empty())
{
st = q.front();
q.pop();

node tmp;
for(int i=0; i<4; i++)
{
for(int j=1;; j++)
{
tmp.x = st.x + j*dx[i];
tmp.y = st.y + j*dy[i];
tmp.turn = st.turn+1;
if(tmp.x<0 || tmp.y<0 ||tmp.x>n+1 ||tmp.y>m+1) break;
if(map[tmp.x][tmp.y])
{
if(tmp.x==x2 && tmp.y==y2) return true;
break;
}
if(vis[tmp.x][tmp.y]) continue;
vis[tmp.x][tmp.y] = 1;
if(tmp.turn<2)  //等于2的没必要加入队列了,他已经是叶子了
q.push(tmp);
}
}
}
return false;
}

int main()
{
//freopen("in.txt","r",stdin);
int t,x1,y1,x2,y2;
while(scanf("%d%d",&n,&m) &&n &&m)  //因为给出n、m为0结束,如果写不等于EOF会超时,错了好几次才反应过来
{
memset(map,0,sizeof(map));
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d",&map[i][j]);
scanf("%d",&t);
int ans=0;
while(t--)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(BFS(x1,y1,x2,y2))
{
ans += 2;
map[x1][y1] = 0;
map[x2][y2] = 0;
}
}
printf("%d\n",ans);
}
return 0;
}


还有另外一种思路,具体看代码:

#include "stdio.h"
int a[102][102];
int x1,y1,x2,y2;
int m,n;
int process()
{
int b[102][102];
int i,j;
int temp,pos;
for(i=0;i<=n+1;i++)
for(j=0;j<=m+1;j++)
b[i][j]=a[i][j];
if(b[x1][y1]!=b[x2][y2] || b[x1][y1]==0) return 0;
temp=1;
while(x1+temp<=n+1)
{
if(b[x1+temp][y1]==0)
{
b[x1+temp][y1]=-1;
temp++;
}
else if(x1+temp==x2 && y1==y2) return 1;
else break;
}
temp=1;
while(x1-temp>=0)
{
if(b[x1-temp][y1]==0)
{
b[x1-temp][y1]=-1;
temp++;
}
else if(x1-temp==x2 && y1==y2) return 1;
else break;
}
temp=1;
while(y1+temp<=m+1)
{
if(b[x1][y1+temp]==0)
{
b[x1][y1+temp]=-1;
temp++;
}
else if(x1==x2 && y1+temp==y2) return 1;
else break;
}
temp=1;
while(y1-temp>=0)
{
if(b[x1][y1-temp]==0)
{
b[x1][y1-temp]=-1;
temp++;
}
else if(x1==x2 && y1-temp==y2) return 1;
else break;
}
temp=1;
while(x2+temp<=n+1)
{
if(b[x2+temp][y2]==-1) return 1;
else if(b[x2+temp][y2]==0)
{
b[x2+temp][y2]=-2;
temp++;
}
else break;
}
temp=1;
while(x2-temp>=0)
{
if(b[x2-temp][y2]==-1) return 1;
else if(b[x2-temp][y2]==0)
{
b[x2-temp][y2]=-2;
temp++;
}
else break;
}
temp=1;
while(y2+temp<=m+1)
{
if(b[x2][y2+temp]==-1) return 1;
else if(b[x2][y2+temp]==0)
{
b[x2][y2+temp]=-2;
temp++;
}
else break;
}
temp=1;
while(y2-temp>=0)
{
if(b[x2][y2-temp]==-1) return 1;
else if(b[x2][y2-temp]==0)
{
b[x2][y2-temp]=-2;
temp++;
}
else break;
}
if(y2<y1)
{
temp=1;
while(x2+temp<=n+1 && b[x2+temp][y2]==-2)
{
pos=1;
while(y2+pos<=m+1 && b[x2+temp][y2+pos]==0)
pos++;
if(y2+pos<=m+1 && b[x2+temp][y2+pos]==-1) return 1;
temp++;
}
temp=1;
while(x2-temp>=0 && b[x2-temp][y2]==-2)
{
pos=1;
while(y2+pos<=m+1 && b[x2-temp][y2+pos]==0)
pos++;
if(y2+pos<=m+1 && b[x2-temp][y2+pos]==-1) return 1;
temp++;
}
}
else if(y2>y1)
{
temp=1;
while(x2+temp<=n+1 && b[x2+temp][y2]==-2)
{
pos=1;
while(y2-pos>=0 && b[x2+temp][y2-pos]==0)
pos++;
if(y2-pos>=0 && b[x2+temp][y2-pos]==-1) return 1;
temp++;
}
temp=1;
while(x2-temp>=0 && b[x2-temp][y2]==-2)
{
pos=1;
while(y2-pos>=0 && b[x2-temp][y2-pos]==0)
pos++;
if(y2-pos>=0 && b[x2-temp][y2-pos]==-1) return 1;
temp++;
}
}
if(x2<x1)
{
temp=1;
while(y2+temp<=m+1 && b[x2][y2+temp]==-2)
{
pos=1;
while(x2+pos<=n+1 && b[x2+pos][y2+temp]==0)
pos++;
if(x2+pos<=n+1 && b[x2+pos][y2+temp]==-1) return 1;
temp++;
}
temp=1;
while(y2-temp>=0 && b[x2][y2-temp]==-2)
{
pos=1;
while(x2+pos<=n+1 && b[x2+pos][y2-temp]==0)
pos++;
if(x2+pos<=n+1 && b[x2+pos][y2-temp]==-1) return 1;
temp++;
}
}
else if(x2>x1)
{
temp=1;
while(y2+temp<=m+1 && b[x2][y2+temp]==-2)
{
pos=1;
while(x2-pos>=0 && b[x2-pos][y2+temp]==0)
pos++;
if(x2-pos>=0 && b[x2-pos][y2+temp]==-1) return 1;
temp++;
}
temp=1;
while(y2-temp>=0 && b[x2][y2-temp]==-2)
{
pos=1;
while(x2-pos>=0 && b[x2-pos][y2-temp]==0)
pos++;
if(x2-pos>=0 && b[x2-pos][y2-temp]==-1) return 1;
temp++;
}
}
return 0;
}
int main()
{
int step;
int i,j;
int num;
scanf("%d%d",&n,&m);
while(n!=0 || m!=0)
{
num=0;
for(i=0;i<=n+1;i++)
for(j=0;j<=m+1;j++)
a[i][j]=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&a[i][j]);
scanf("%d",&step);
for(i=0;i<step;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(process()==1)
{
a[x1][y1]=0;a[x2][y2]=0;
num=num+2;
}
}
printf("%d\n",num);
scanf("%d%d",&n,&m);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: