标签:des style blog http io ar color os sp
Description
Input
Output
Sample Input
Sample Output
#include<stdio.h> char map[2100][2100]; int N,M; int min; struct weak { int x,y; }point[2100]; int sign; void make_sign() { int i,j; for(i=0;i<N;i++) for(j=0;j<M;j++) if(map[i][j]==‘.‘){ point[sign].x=i; point[sign++].y=j; } } int seach_all() { for(int i=0;i<N;i++) for(int j=0;j<M;j++) if(map[i][j]==‘.‘) return 0; return 1; } void light1(int x,int y) { map[x][y]=‘1‘; if(x-1>=0) map[x-1][y]=‘1‘; if(y+1<M) map[x][y+1]=‘1‘; } void light2(int x,int y) { map[x][y]=‘1‘; if(x+1<N) map[x+1][y]=‘1‘; if(y+1<M) map[x][y+1]=‘1‘; } void light3(int x,int y) { map[x][y]=‘1‘; if(x+1<N) map[x+1][y]=‘1‘; if(y-1>=0) map[x][y-1]=‘1‘; } void light4(int x,int y) { map[x][y]=‘1‘; if(x-1>=0) map[x-1][y]=‘1‘; if(y-1>=0) map[x][y-1]=‘1‘; } void DFS(int pt,int step) { if(pt>=sign) return; if(seach_all()){ if(min>step) min=step; return; } int i; char flag[5]; for(i=0;i<=4;i++){ if(i==0) DFS(pt+1,step); if(i==1&&(map[point[pt].x-1][point[pt].y]!=‘#‘||map[point[pt].x][point[pt].y+1]!=‘#‘)){ flag[1]=map[point[pt].x][point[pt].y]; flag[2]=map[point[pt].x-1][point[pt].y]; flag[3]=map[point[pt].x][point[pt].y+1]; light1(point[pt].x,point[pt].y); DFS(pt+1,step+1); map[point[pt].x][point[pt].y]=flag[1]; map[point[pt].x-1][point[pt].y]=flag[2]; map[point[pt].x][point[pt].y+1]=flag[3]; } if(i==2&&(map[point[pt].x+1][point[pt].y]!=‘#‘||map[point[pt].x][point[pt].y+1]!=‘#‘)){ flag[1]=map[point[pt].x][point[pt].y]; flag[2]=map[point[pt].x+1][point[pt].y]; flag[3]=map[point[pt].x][point[pt].y+1]; light2(point[pt].x,point[pt].y); DFS(pt+1,step+1); map[point[pt].x][point[pt].y]=flag[1]; map[point[pt].x+1][point[pt].y]=flag[2]; map[point[pt].x][point[pt].y+1]=flag[3]; } if(i==3&&(map[point[pt].x+1][point[pt].y]!=‘#‘||map[point[pt].x][point[pt].y-1]!=‘#‘)){ flag[1]=map[point[pt].x][point[pt].y]; flag[2]=map[point[pt].x+1][point[pt].y]; flag[3]=map[point[pt].x][point[pt].y-1]; light3(point[pt].x,point[pt].y); DFS(pt+1,step+1); map[point[pt].x][point[pt].y]=flag[1]; map[point[pt].x+1][point[pt].y]=flag[2]; map[point[pt].x][point[pt].y-1]=flag[3]; } if(i==4&&(map[point[pt].x-1][point[pt].y]!=‘#‘||map[point[pt].x][point[pt].y-1]!=‘#‘)){ flag[1]=map[point[pt].x][point[pt].y]; flag[2]=map[point[pt].x-1][point[pt].y]; flag[3]=map[point[pt].x][point[pt].y-1]; light4(point[pt].x,point[pt].y); DFS(pt+1,step+1); map[point[pt].x][point[pt].y]=flag[1]; map[point[pt].x-1][point[pt].y]=flag[2]; map[point[pt].x][point[pt].y-1]=flag[3]; } } } int main() { while(scanf("%d%d",&N,&M)!=EOF){ if(N==0&&M==0) return 0; min=9999999; for(int i=0;i<N;i++) scanf("%s",map[i]); sign=0; make_sign(); if(sign) DFS(0,0); if(min==9999999) min=-1; printf("%d\n",min); } return 0; }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/yanglingwell/p/4133088.html