问题 B: [Usaco2007 Open]Fliptile 翻格子游戏
时间限制: 5 Sec 内存限制: 128 MB题目描述
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M x N grid (1 <= M <= 15; 1 <= N <= 15) of square tiles, each of which is colored black on one side and white on the other side. As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make. Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".
输入
* Line 1: Two space-separated integers: M and N
* Lines 2..M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white
输出
* Lines 1..M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
样例输入
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
样例输出
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0OUTPUT DETAILS:After flipping at row 2 column 1, the board will look like:
0 0 0 1
1 0 1 0
1 1 1 0
1 0 0 1After flipping at row 2 column 4, the board will look like:
0 0 0 0
1 0 0 1
1 1 1 1
1 0 0 1After flipping at row 3 column 1, the board will look like:
0 0 0 0
0 0 0 1
0 0 1 1
0 0 0 1After flipping at row 3 column 4, the board will look like:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0Another solution might be:
0 1 1 0
0 0 0 0
0 0 0 0
0 1 1 0
but this solution is lexicographically higher than the solution above.
80分高斯
#include<iostream> #include<cmath> #include<cstring> #include<cstdio> #include<queue> #include<set> #include<vector> #include<map> #include<cstdlib> #include<algorithm> #define V 17 #define mod 1000000007 #define LL long long using namespace std; //set<int>::iterator it; //priority_queue<int,vector<int>,greater<int> >q1; int m,n;// int g[V][V],id,s[V*V][V*V],ans[V*V],a[V][V]; inline int sol() {int i,col,sd=0;for(i=1,col=1;i<=g[n][m],col<=g[n][m]+1;i++,col++){sd=0;if(!s[i][col])for(int j=i+1;j<=g[n][m];j++){if(s[j][col]){for(int k=col;k<=g[n][m]+1;k++)swap(s[i][k],s[j][k]);break; } } if(s[i][col]==0){i--;continue; }for(int j=i+1;j<=g[n][m];j++){if(!s[j][col])continue;for(int k=col;k<=g[n][m]+1;k++){s[j][k]^=s[i][k]; } }} int tmp;for(int i=g[n][m];i>=1;i--) {tmp=s[i][g[n][m]+1]; for(int j=i+1;j<=g[n][m];j++) if(s[i][j]&&ans[j]) tmp^=ans[j];if(s[i][i]==0)return 0;ans[i]=tmp; }return 1; } inline int haha() {// freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);//freopen("fliptile.in","r",stdin); freopen("fliptile.out","w",stdout);cin>>n>>m;for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){cin>>a[i][j];g[i][j]=++id;}}for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++){if(j>1)s[g[i][j]][g[i][j-1]]=1;if(j<m)s[g[i][j]][g[i][j+1]]=1;if(i>1)s[g[i][j]][g[i-1][j]]=1;if(i<n)s[g[i][j]][g[i+1][j]]=1;s[g[i][j]][g[i][j]]=1;s[g[i][j]][g[n][m]+1]=a[i][j];}} if(sol())for(int i=1;i<=n;i++) {for(int j=1;j<=m;j++)cout<<ans[g[i][j]]<<" ";cout<<endl;}else cout<<"IMPOSSIBLE";return 0; } int gg=haha(); int main() {;}
AC:
#include<iostream> #include<cmath> #include<cstring> #include<cstdio> #include<queue> #include<set> #include<vector> #include<map> #include<cstdlib> #include<algorithm> #define V 20 #define mod 1000000007 #define LL long long using namespace std; //set<int>::iterator it; //priority_queue<int,vector<int>,greater<int> >q1; int m,n;// int g[V][V],id,s[V][V],ans=V*V*V,a[V][V]; inline void check() {int sd=0,sb=0;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)sb+=a[i][j];if(ans>sb){for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)g[i][j]=a[i][j];ans=sb; return ;} if(sb>ans)return;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)if(g[i][j]==0&&a[i][j]!=0)return;else if(a[i][j]==0&&g[i][j]!=0){sd=1;break;}if(sd)break;}for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)g[i][j]=a[i][j]; } inline void dfs(int x) {for(int i=1;i<=m;i++){if(i==1){if((a[x-2][1]+a[x-1][1]+a[x-1][2]+s[x-1][1])%2==0)a[x][1]=0;else a[x][1]=1;}else if(i<m){if((a[x-2][i]+a[x-1][i]+a[x-1][i-1]+s[x-1][i]+a[x-1][i+1])%2==0)a[x][i]=0;else a[x][i]=1; }else{if((a[x-2][i]+a[x-1][i]+a[x-1][i-1]+s[x-1][i])%2==0)a[x][i]=0;else a[x][i]=1; }}if(x<n)dfs(x+1);if(x==n){for(int i=1;i<=m;i++){if(i==1){if((a[x-1][1]+a[x][1]+a[x][2]+s[x][1])%2!=0)return ;}else if(i<m){if((a[x-1][i]+a[x][i-1]+s[x][i]+a[x][i+1]+a[x][i])%2!=0)return ;}else{if((a[x][i]+a[x-1][i]+a[x][i-1]+s[x][i])%2!=0)return ;}}check();} } inline int haha() { // freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);//freopen("fliptile.in","r",stdin); freopen("fliptile.out","w",stdout);cin>>n>>m;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){scanf("%d",&a[i][j]);g[i][j]=1;}if(n*m==1){if(a[1][1]==0)printf("0");else printf("1");return 0;}memcpy(s,a,sizeof(s));for(int p=0;p<(1<<m);p++){ memset(a,0,sizeof(a));for(int i=1;i<=m;i++)if((1<<i-1)&p){a[1][i]=1;}for(int i=1;i<=m;i++){if(i==1){if((a[1][1]+a[1][2]+s[1][1])%2==0)a[2][1]=0;else a[2][1]=1;}else if(i<m){if((a[1][i]+a[1][i-1]+s[1][i]+a[1][i+1])%2==0)a[2][i]=0;else a[2][i]=1; }else{if((a[1][i]+a[1][i-1]+s[1][i])%2==0)a[2][i]=0;else a[2][i]=1; }}dfs(3);// memcpy(a,s,sizeof(a));}if(ans==V*V*V){printf("IMPOSSIBLE");return 0;}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)printf("%d ",g[i][j]);printf("\n");}return 0; } int gg=haha(); int main() {;}