A
状压暴力
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> pii; const int dx[2][4]={1,-1,-1,1,0,0,0,0}; const int dy[2][4]={0,0,0,0,1,1,-1,-1}; const int inf=0x3f3f3f3f; int n,m,sz,res,H[205][205]; vector<pii> vec; char s[205][205]; inline int light(int u,int st,int dir) { st|=(1<<u); int x=vec[u].first,y=vec[u].second; for (int i=0;i<2;++i) { int tx=x+dx[i][dir],ty=y+dy[i][dir]; if (tx<0||tx>=n||ty<0||ty>=m) continue; if (s[tx][ty]==‘#‘) return -1; st|=(1<<H[tx][ty]); } return st; } void dfs(int u,int st,int now,int cnt) { if (st==sz) { res=min(res,cnt); return; } if (u==(int)vec.size()) return; if (u==now) { for (int i=0;i<4;++i) { int nxt=light(u,st,i); if (nxt==-1) continue; dfs(u+1,nxt,now,cnt+1); } } else { int nxt=light(u,st,1); if (nxt!=-1) dfs(u+1,nxt,now,cnt+1); } dfs(u+1,st,now,cnt); } int main() { while (scanf("%d%d",&n,&m)==2&&n+m) { memset(H,-1,sizeof H); for (int i=0;i<n;++i) scanf("%s",s[i]); vec.clear(); int cnt=0; for (int i=0;i<n;++i) for (int j=0;j<m;++j) if (s[i][j]==‘.‘) { vec.push_back(pii(i,j)); H[i][j]=cnt++; } if (cnt==0) { puts("0"); continue; } sz=vec.size(); sz=(1<<sz)-1; res=inf; for (int i=0;i<(int)vec.size();++i) dfs(0,0,i,0); printf("%d\n",res==inf?-1:res); } return 0; }
B
爆搜图基本和A一样
#include <bits/stdc++.h> using namespace std; struct node { int x,y,st; node(){} node(int x,int y,int st):x(x),y(y),st(st){} }; queue<node> que; const int maxn=105; const int inf=0x3f3f3f3f; const int dx[4]={-1,1,0,0}; const int dy[4]={0,0,-1,1}; int n,m,t,st,X[5],Y[5],vis[105][105][16]; char s[105][105]; inline int bfs(int xx,int yy) { while (!que.empty()) que.pop(); for (int i=0;i<n;++i) for (int j=0;j<m;++j) for (int k=0;k<st;++k) vis[i][j][k]=inf; int temp=0; for (int i=0;i<t;++i) if (xx==X[i]&&yy==Y[i]) temp|=(1<<i); vis[xx][yy][temp]=0; que.push(node(xx,yy,temp)); while (!que.empty()) { int x=que.front().x,y=que.front().y,now=que.front().st; que.pop(); for (int i=0;i<4;++i) { int tx=x+dx[i],ty=y+dy[i],nxt=now; if (tx<0||tx>=n||ty<0||ty>=m||s[tx][ty]==‘#‘) continue; for (int j=0;j<t;++j) if (tx==X[j]&&ty==Y[j]) nxt|=(1<<j); if (nxt==(st-1)) return vis[x][y][now]+1; if (vis[tx][ty][nxt]!=inf) continue; vis[tx][ty][nxt]=vis[x][y][now]+1; que.push(node(tx,ty,nxt)); } } return -1; } int main() { while (scanf("%d%d",&n,&m)==2&&n+m!=0) { for (int i=0;i<n;++i) scanf("%s",s[i]); scanf("%d",&t); st=1<<t; for (int i=0;i<t;++i) { scanf("%d%d",&X[i],&Y[i]); --X[i],--Y[i]; } for (int i=0;i<n;++i) for (int j=0;j<m;++j) if (s[i][j]==‘@‘) printf("%d\n",bfs(i,j)); } return 0; }
C
#include <bits/stdc++.h> using namespace std; int n,s1[305][305],s2[305][305],s3[305][305]; inline int compare() { int ret=0; for (int i=0;i<n;++i) for (int j=0;j<n;++j) if (s1[i][j]==s2[i][j]) ++ret; return ret; } inline void flip() { for (int i=0;i<n;++i) for (int j=0;j<n;++j) s3[j][n-i-1]=s1[i][j]; for (int i=0;i<n;++i) for (int j=0;j<n;++j) s1[i][j]=s3[i][j]; } int main() { while (scanf("%d",&n)==1&&n!=0) { for (int i=0;i<n;++i) for (int j=0;j<n;++j) scanf("%d",&s1[i][j]); for (int i=0;i<n;++i) for (int j=0;j<n;++j) scanf("%d",&s2[i][j]); int res=0; for (int i=0;i<4;++i) { res=max(res,compare()); flip(); } printf("%d\n",res); } return 0; }
I
DP
#include <bits/stdc++.h> using namespace std; const int inf=0x3f3f3f3f; int g,b,s,bag[50][10],dp[2][1<<21]; int vec[10]; int dfs(int u,int st) { if (st==(1<<b)-1) { return 0; } int tmp[10]; if (u==0&&dp[u][st]!=-inf) return dp[u][st]; if (u==1&&dp[u][st]!=inf) return dp[u][st]; int ret=(u==0)?-inf:inf; for (int i=0;i<b;++i) if (!(st&(1<<i))) { for (int j=0;j<g;++j) tmp[j] = vec[j]; for (int j=0;j<g;++j) vec[j]+=bag[i][j]; int flag=0; for (int j=0;j<g;++j) if (vec[j]>=s) { flag+=vec[j]/s; vec[j] %=s; } if (!flag) { if (u==0) ret=max(ret,dfs(!u,st|(1<<i))); else ret=min(ret,dfs(!u,st|(1<<i))); } else { if (u==0) ret=max(ret,dfs(u,st|(1<<i))+flag); else ret=min(ret,dfs(u,st|(1<<i))-flag); } for (int j=0;j<g;++j) vec[j] = tmp[j]; } return dp[u][st]=ret; } int main() { while (scanf("%d%d%d",&g,&b,&s)==3&&(g!=0||b!=0||s!=0)) { memset(bag,0,sizeof bag); memset(vec,0,sizeof(vec)); for (int i=0;i<b;++i) { int n; scanf("%d",&n); for (int j=0;j<n;++j) { int c; scanf("%d",&c); --c; ++bag[i][c]; } } for (int i=0;i<(1<<b);++i) { dp[0][i]=-inf; dp[1][i]=inf; } printf("%d\n",dfs(0,0)); } return 0; }
时间: 2024-10-09 10:12:43