vis判重 几个值分别表示 日期 左上角所在位置(只会在9个地方出现) 人间四个角干旱的天数。
因为day用位运算记录的状态,第一个点在最开头,要注意。
#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
int vis[370][10][8][8][8][8];
int day[370],n;
int dx[10]={0,0,1,2,-1,-2,0,0,0,0};
int dy[10]={0,0,0,0,0,0,1,2,-1,-2};
struct node
{
int d1,d2,d3,d4;
};
int get(int x,int y)
{
int num=(x-1)*4+y;
return (1<<(16-num));
}
bool check(int date,int x,int y,node corner)
{
if(corner.d1==7||corner.d2==7||corner.d3==7||corner.d4==7)
{
return false;
}
int flag=0;
flag|=get(x,y);
flag|=get(x,y+1);
flag|=get(x+1,y);
flag|=get(x+1,y+1);
if(flag&day[date])
{
return false;
}
if(vis[date][(x-1)*3+y][corner.d1][corner.d2][corner.d3][corner.d4])
{
return false;
}
vis[date][(x-1)*3+y][corner.d1][corner.d2][corner.d3][corner.d4]=true;
return true;
}
bool dfs(int date,int x,int y,node corner)
{
if(date==n+1)
{
return true;
}
if(!check(date,x,y,corner))
{
return false;
}
int xx,yy;
node tmp;
for(int i=1;i<=9;i++)
{
tmp=corner;
tmp.d1++;
tmp.d2++;
tmp.d3++;
tmp.d4++;
xx=x+dx[i];
yy=y+dy[i];
if(xx>=1&&xx<=3&&yy>=1&&yy<=3)
{
if(xx==1&&yy==1)
{
tmp.d1=0;
}
if(xx==1&&yy==3)
{
tmp.d2=0;
}
if(xx==3&&yy==1)
{
tmp.d3=0;
}
if(xx==3&&yy==3)
{
tmp.d4=0;
}
if(dfs(date+1,xx,yy,tmp))
{
return true;
}
}
}
return false;
}
int main()
{
while(scanf("%d",&n))
{
if(n==0)
{
break;
}
int tmp;
for(int i=1;i<=n;i++)
{
day[i]=0;
for(int j=1;j<=16;j++)
{
scanf("%d",&tmp);
day[i]<<=1;
day[i]|=tmp;
}
}
memset(vis,0,sizeof(vis));
node start;
start.d1=1;
start.d2=1;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