| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
一次ac,贴代码#include<iostream>
#include<fstream>
#include<queue>
using namespace std;
int map[80][80];
int v[80][80];
int move[4][2]={0,1,0,-1,1,0,-1,0};
int n,m,x,y,x0,y0;
struct node{
int s,t,k,f;
friend bool operator <(node a,node b){
return(a.k>b.k);
}
};
int ok(int s,int t){
if(s>=0&&s<=n&&t>=0&&t<=m&&map[s][t]==0)
return(1);
return(0);
}
int solve(){
int i,j,k,t,f;
priority_queue<node> a;
node b;
for(i=0;i<4;i++)
{
b.s=x+move[i][0];
b.t=y+move[i][1];
if(ok(b.s,b.t))
{
b.f=i;
b.k=1;
a.push(b);
}
}
while(!a.empty())
{
b=a.top();
a.pop();
i=b.s;
j=b.t;
if(i==x0&&j==y0)
{
return(b.k);
}
k=b.k;
f=b.f;
if(v[i][j])
continue;
v[i][j]=1;
for(t=0;t<4;t++)
{
i+=move[t][0];
j+=move[t][1];
if(v[i][j]==0&&ok(i,j))
{
b.s=i;
b.t=j;
b.f=t;
if(b.f==f)
b.k=k;
else
b.k=k+1;
a.push(b);
}
i-=move[t][0];
j-=move[t][1];
}
}
return(0);
}
int main(){
//ifstream cin("in.txt");
int i,j,k,h=0;
while(1){
cin>>m>>n;
h++;
if(n==0&&m==0)
return(0);
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
cin.get();
for(j=1;j<=m;j++)
{
if(cin.get()=='X')
map[i][j]=1;
}
}
n++;
m++;
cout<<"Board #"<<h<<":"<<endl;
i=0;
while(1)
{
i++;
cin>>y>>x>>y0>>x0;
j=map[x0][y0];
map[x0][y0]=0;
if(y==0)
break;
memset(v,0,sizeof(v));
k=solve();
map[x0][y0]=j;
if(k!=0)
cout<<"Pair "<<i<<": "<<k<<" segments."<<endl;
else
cout<<"Pair "<<i<<": impossible."<<endl;
}
cout<<endl;
}
return(0);
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator