标签:des style blog http color io os ar java
又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性。
11779687 | 2014-10-02 20:57:53 | Accepted | 4770 | 0MS | 496K | 2976 B | G++ | czy |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360 Accepted Submission(s): 392
好像状压更简单:
http://www.cnblogs.com/kuangbin/p/3416163.html
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<string> 10 //#include<pair> 11 12 #define N 205 13 #define M 15 14 #define mod 10000007 15 //#define p 10000007 16 #define mod2 100000000 17 #define ll long long 18 #define LL long long 19 #define maxi(a,b) (a)>(b)? (a) : (b) 20 #define mini(a,b) (a)<(b)? (a) : (b) 21 22 using namespace std; 23 24 int n,m; 25 int ans; 26 int k; 27 int vis[N][N]; 28 char s[N][N]; 29 int c[N]; 30 int step[4][3][2]={ {-1,0,0,0,0,1}, 31 {0,-1,-1,0,0,0}, 32 {0,-1,0,0,1,0}, 33 {0,0,0,1,1,0} 34 }; 35 typedef struct 36 { 37 int x; 38 int y; 39 }PP; 40 41 PP p[N]; 42 43 void change(int x,int y,int st[3][2],int s) 44 { 45 int i; 46 int nx,ny; 47 for(i=0;i<3;i++){ 48 nx=x+st[i][0]; 49 ny=y+st[i][1]; 50 if(nx<0 || nx>=n) continue; 51 if(ny<0 || ny>=m) continue; 52 53 vis[nx][ny]=s; 54 } 55 } 56 57 int judge(int x,int y,int st[3][2]) 58 { 59 int i; 60 int nx,ny; 61 for(i=0;i<3;i++){ 62 nx=x+st[i][0]; 63 ny=y+st[i][1]; 64 if(nx<0 || nx>=n || ny<0 || ny>=m) continue; 65 if(s[nx][ny]==‘#‘){ 66 return 0; 67 } 68 } 69 return 1; 70 } 71 72 void ini() 73 { 74 int i,j; 75 memset(c,0,sizeof(c)); 76 ans=20; 77 k=0; 78 for(i=0;i<n;i++){ 79 scanf("%s",s[i]); 80 } 81 for(i=n-1;i>=0;i--){ 82 for(j=0;j<m;j++){ 83 if(s[i][j]==‘.‘){ 84 k++; 85 p[k].x=i;p[k].y=j; 86 } 87 } 88 } 89 90 for(i=1;i<=k;i++){ 91 if(judge(p[i].x,p[i].y,step[0])==1){ 92 c[i]=1; 93 } 94 else{ 95 c[i]=-1; 96 } 97 } 98 } 99 100 101 void dfs(int i,int f,int re) 102 { 103 if(re>=ans){ 104 return; 105 } 106 if(i==k+1){ 107 ans=re; 108 return; 109 } 110 if(vis[ p[i].x ][ p[i].y ]==1) 111 { 112 dfs(i+1,f,re); 113 } 114 if(f!=i && c[i]==1){ 115 change(p[i].x,p[i].y,step[0],1); 116 dfs(i+1,f,re+1); 117 change(p[i].x,p[i].y,step[0],0); 118 } 119 } 120 121 void solve() 122 { 123 int i,j; 124 if(k==0){ 125 ans=0;return; 126 } 127 memset(vis,0,sizeof(vis)); 128 dfs(1,0,0); 129 for(j=1;j<=3;j++) 130 { 131 memset(vis,0,sizeof(vis)); 132 for(i=1;i<=k;i++){ 133 if(judge( p[i].x,p[i].y,step[j] )==1){ 134 change(p[i].x,p[i].y,step[j],1); 135 dfs(1,i,1); 136 change(p[i].x,p[i].y,step[j],0); 137 } 138 } 139 } 140 } 141 142 143 void out() 144 { 145 if(ans==20) ans=-1; 146 printf("%d\n",ans); 147 } 148 149 int main() 150 { 151 // freopen("data.in","r",stdin); 152 //freopen("data.out","w",stdout); 153 //scanf("%d",&T); 154 // for(int ccnt=1;ccnt<=T;ccnt++) 155 // while(T--) 156 while(scanf("%d%d",&n,&m)!=EOF) 157 { 158 if(n==0 && m==0 ) break; 159 //printf("Case %d: ",ccnt); 160 ini(); 161 solve(); 162 out(); 163 } 164 165 return 0; 166 }
HDU 4770 Lights Against Dudely 暴力枚举+dfs
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/njczy2010/p/4004461.html