AC日记——Sagheer, the Hausmeister codeforces 812b
812B - Sagheer, the Hausmeister
思路:
搜索;
代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 20 #define maxm 105 #define INF 0x7fffffff int n,m,deep[maxn][2],num[maxn],ans=INF,k,sum[maxn],cnt; char map[maxn][maxm]; void dfs(int now,bool st,int val) { if(val>=ans) return; if(now==n) { if(num[now]) { if(st) val+=k-deep[now][0]; else val+=deep[now][1]-1; } if(val<ans) ans=val; return; } if(sum[now]==cnt) { if(num[now]) { if(st) val+=k-deep[now][0]; else val+=deep[now][1]-1; } if(val<ans) ans=val; return; } if(num[now]) { if(st) dfs(now+1,st^1,val+k),dfs(now+1,st,val+(k-deep[now][0])*2+1); else dfs(now+1,st,val+deep[now][1]*2-1),dfs(now+1,st^1,val+k); } else dfs(now+1,st,val+1),dfs(now+1,st^1,val+k); } int main() { scanf("%d%d",&n,&m);k=m+2; for(int i=1,i_=n;i<=n;i++,i_=n-i+1) { scanf("%s",map[i]+1); for(int j=1;j<=m+2;j++) { if(map[i][j]=='1'&&(!deep[i_][0])) deep[i_][0]=j; if(map[i][j]=='1') deep[i_][1]=j,num[i_]++; } } for(int i=1;i<=n;i++) sum[i]=sum[i-1]+num[i],cnt+=num[i]; dfs(1,false,0),printf("%d\n",ans); return 0; }