dhy0077 | Date: Thursday, 01.16.2014, 11:22 AM | Message # 1 |
Private
Group: Administrators
Messages: 9
Status: Offline
| Code //b3041
#include<cstdio> #include<cstdlib> #include<cstring> #include<set> #include<algorithm> #include<map> #include<vector> #include<queue> #include<iostream> #include<string> #include<cmath> #define N 9 #define FOR(i,a,b) for(i=(a);i<=(b);i++) #define ROF(i,a,b) for(i=(a);i>=(b);i--) #define mmt(a,b) memset(a,b,sizeof(a)) typedef long long LL; using namespace std; int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; struct poi{int x,y;}tl[N*N+10]; bool f; void floodfill(int x,int y,int c2) { if (a[x][y]==c2) return; a[x][y]=c2; FOR(i,0,3) { int tx=x+dx[i],ty=y+dy[i]; if (tx&&ty&&tx<=n&&ty<=n) floodfill(tx,ty,c1,c2); } } int ok(int x,int y) { if (col[x][y]==-1) return 0; FOR(i,0,3) { int tx=x+dx[i],ty=y+dy[i]; if (tx&&ty&&tx<=n&&ty<=n) if (col[tx][ty]==-1) return i; } return -1; } void DFS(int step,) { if (f) return; if (nc==0) {f=1;return;} if (step+nc>dep) return; poi list[N*N+5]; int i; FOR(i,1, } int IDAStar() { mmt(col,0);int i,j; FOR(i,1,n) FOR(j,1,n) col[a[i][j]]++; FOR(i,0,5) if (col[i]) ccnt++; if (ccnt==1) return 0; floodfill(1,1,-1); int Len=0; FOR(i,1,n) FOR(j,1,n) if (ok(i,j)!=-1) tl[++Len].x=i,tl[Len].y=j; for(dep=1;;dep++) { nc=ccnt-1; memcpy(a2,a,sizeof(a)); f=0; DFS(0); if (f) return dep; } } void Main() { scanf("%d",&n); FOR(i,1,n) FOR(j,1,n) scanf("%d",&a[i][j]); int ans=IDAStar(); printf("%d\n",ans); } int main() { scanf("%d",&T); while (T--) Main(); }
|
|
| |
dhy0077 | Date: Thursday, 01.16.2014, 11:36 AM | Message # 2 |
Private
Group: Administrators
Messages: 9
Status: Offline
| 2楼
|
|
| |
dhy0077 | Date: Thursday, 01.16.2014, 4:53 PM | Message # 3 |
Private
Group: Administrators
Messages: 9
Status: Offline
| b3030.cpp
|
|
| |