广度优先搜索。注意题目中说结果可能很大,但是实际上是到不了1e9+9的,有些题目就是故意吓唬人。
代码:
#include <iostream> //分开也是可以的吧。
#include <queue>
#include <cstring>
using namespace std;
typedef long long ll;
int map[11][10010]; //寻求蛛丝马迹拼凑而成。
const int mod=1000000009;
int z=90001;
struct node
{
int x,y;
char dir;
char flag;
};
int main()
{
int n,m,k;
while(~scanf("%d %d %d",&n,&m,&k))
{
// z=90001;
memset(map,0,sizeof(map));
int a,b,c,d;
for(int i=1;i<=k;i++)
{
scanf("%d %d %d %d",&a,&b,&c,&d);
map[a][b]=i;
map[c][d]=i;
}
//之后进行广度优先搜索。
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
if(map[i][j]==0)
{
a=i;b=j;
c=-1;
break;
}
if(c==-1)
break;
}
node temp1;
temp1.x=a; temp1.y=b; temp1.dir ='O';
queue<node>q; //应该是有条件限制的。 加上条件限制时候用最为笨的方法实现。
q.push(temp1);
ll ans=0;
while( !q.empty() )
{
node temp=q.front();
q.pop(); //通过迷宫中的移动方式应该也是可以。
if(temp.dir =='O'); //首先的目的就是进行移动,这是第一步,之后还是有其它的方式。
else
{
if(temp.dir=='U') //U,D,L,R; 移动错误。
map[temp.x+2][temp.y]=mod/*map[temp.x][temp.y]*/,map[temp.x][temp.y]=0;
else if(temp.dir=='D')
map[temp.x-2][temp.y]=mod/*map[temp.x][temp.y]*/,map[temp.x][temp.y]=0;
else if(temp.dir=='L')
map[temp.x][temp.y+2]=mod/*map[temp.x][temp.y]*/,map[temp.x][temp.y]=0; //先不进行考虑。
else if(temp.dir=='R')
map[temp.x][temp.y-2]=mod/*map[temp.x][temp.y]*/,map[temp.x][temp.y]=0;
}
if( temp.x-2>=1&&map[temp.x-1][temp.y]==map[temp.x-2][temp.y] )
{
ans=(ans+1)%mod;
node temp2;
temp2.dir='U',temp2.x=temp.x-2,temp2.y=temp.y;
q.push(temp2);
}
if(temp.x+2<=n&& map[temp.x+2][temp.y]==map[temp.x+1][temp.y] ) //对啊,这里是永远也是不能结束的。
{
ans=(ans+1)%mod;
node temp4;
temp4.dir='D',temp4.x=temp.x+2,temp4.y=temp.y;
q.push(temp4);
}
if(temp.y-2>=1&& map[temp.x][temp.y-2]==map[temp.x][temp.y-1] )
{
ans=(ans+1)%mod;
node temp6;
temp6.dir='L',temp6.x=temp.x,temp6.y=temp.y-2;
q.push(temp6);
}
if( temp.y+2<=m&&map[temp.x][temp.y+1]==map[temp.x][temp.y+2] )
{
ans=(ans+1)%mod;
node temp8;
temp8.dir='R',temp8.x=temp.x,temp8.y=temp.y+2;
q.push(temp8);
}
}
cout<<ans<<endl;
}
return 0;
}