(麻烦)Flip Game (CD1488)
来源:互联网 发布:软件开发案例分析 编辑:程序博客网 时间:2024/05/18 12:34
太麻烦了,刚开始自己还想歪了.后来也是各种细节出错.值得高兴的是做出来了,黑黑
最近又做了一遍,发现自己的思路更清析了,不过还是WA了好几盘。因为在flip的时候应该是flip下一个坐标的
#include<iostream>#include<cstdio>using namespace std;#define inf 10000char s[15][15];int go[16][16];int a[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int ans;void put(){cout<<endl;for (int i=1;i<=4;i++){for (int j=1;j<=4;j++)cout<<go[i][j];cout<<endl;}}bool isok(){int i,j,sum=0;for (i=1;i<=4;i++)for (j=1;j<=4;j++)sum+=go[i][j];return sum==0 || sum==16;}void flip(int x,int y){for (int i=0;i<4;i++)go[x+a[i][0]][y+a[i][1]]^=1;go[x][y]^=1;}void dfs(int x,int y,int s){//cout<<x<<' '<<y<<' '<<s<<endl;if (isok()){ // cout<<s<<endl;ans=min(ans,s); return;}int nextx=x,nexty=y+1;if (nexty>4){nexty=1;nextx++;}if (nextx>4)return; flip(nextx,nexty);dfs(nextx,nexty,s+1);flip(nextx,nexty);dfs(nextx,nexty,s);}int main(){freopen("in.txt","r",stdin);int i,j,k;for (i=0;i<4;i++)scanf ("%s",s[i]);for (i=0;i<4;i++)for (j=0;j<4;j++)go[i+1][j+1]=s[i][j]=='b'?1:0;ans=inf;dfs(1,0,0);if (ans==inf)puts("Impossible");elseprintf("%d\n",ans);return 0;}
老版本
#include<iostream>#include<stdlib.h>#include<math.h>#include<stdio.h>#include<algorithm>#include<queue>#include<string.h>#include<stack>#include<math.h>#include<string>#include<stdlib.h>#include<list>#include<vector>using namespace std;char map2[5][5];int map[5][5];int a[4][2]={{0,1},{1,0},{-1,0},{0,-1}};bool f;int vist[4][4];int step;int hi[4][4];bool in(int x,int y){if (x<0||x>3||y<0||y>3)return false;return true;}void change(int x,int y){int i,j,k;map[x][y]=1-map[x][y];for (k=0;k<4;k++){i=a[k][0]+x;j=a[k][1]+y;if (in(i,j))map[i][j]=1-map[i][j];}}int get(){int i,j;int sum=0;for (i=0;i<4;i++)for (j=0;j<4;j++)sum+=map[i][j];return sum;}void dfs(int v){int i,j,k;for (i=1;i<4;i++){for (j=0;j<4;j++){if (map[i-1][j]!=v){//cout<<i<<' '<<j<<' '<<v<<' '<<map[i-1][j]<<endl;change(i,j);step++;}}}}void dfs2(){int i,j,k;k=0;for (i=0;i<4;i++){for (j=0;j<4;j++){//cout<<vist[i][j]<<' ';if (vist[i][j])change(i,j);}//cout<<endl;}//cout<<endl;}int num(){int sum=0;int i,j;for (i=0;i<4;i++){for (j=0;j<4;j++){if (vist[i][j])sum++;//cout<<vist[i][j]<<' ';}//cout<<endl;}//cout<<endl;return sum;}int main(){freopen("fuck.txt","r",stdin);int i,j,k;for (i=0;i<4;i++)gets(map2[i]);k=0;for (i=0;i<4;i++){for (j=0;j<4;j++)map[i][j]=map2[i][j]=='b'?0:1;}for (i=0;i<4;i++){for (j=0;j<4;j++)hi[i][j]=map[i][j];}int make[4];for (i=0;i<4;i++)map[0][i]=make[i]=0;int t=0;int ans=100;memset(vist,0,sizeof(vist));int tt=1<<16+1;int v[4];memset(v,0,sizeof(v));while (tt--){for (i=0;i<4;i++){k=1;for (j=0;j<4;j++){vist[i][j]=v[i]&k;k=k<<1;}}dfs2();i=0;j=get();if (j==0||j==16)ans=min(ans,num());while (i<4){if (v[i]<15){v[i]++;break;}elsev[i++]=0;}for (i=0;i<4;i++){for (j=0;j<4;j++)map[i][j]=hi[i][j];}}/*while (t<4){k=0;for (i=0;i<4;i++)if (hi[0][i]!=map[0][i]){change(0,i);k++;}step=k;dfs(0);cout<<endl<<endl;for (i=0;i<4;i++){for (j=0;j<4;j++)cout<<map[i][j]<<' ';cout<<endl;}cout<<endl;int temp=get();if (temp==0)ans=min(ans,step);step=k;dfs(1);cout<<endl<<endl;for (i=0;i<4;i++){for (j=0;j<4;j++)cout<<map[i][j]<<' ';cout<<endl;}cout<<endl;temp=get();if (temp==4)ans=min(ans,step);t=0;i=0;while (i<4){if (make[i]==0){make[i]=1;i=54;}else{make[i]=0;i++;}}for (i=0;i<4;i++)t+=make[i],map[0][i]=make[i];cout<<endl;for (i=1;i<4;i++)for (j=0;j<4;j++)map[i][j]=hi[i][j];//t=24;}*/if (ans<100)printf("%d\n",ans);else printf("Impossible\n");}
Flip Game
Time Limit: 1000 ms Memory Limit: 65536 kB Solved:56Tried: 171
Description
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
Choose any one of the 16 pieces.
Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
There are only one test case in the input file.
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
Output
Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
Simple Input
bwwb
bbwb
bwwb
bwww
Simple Output
4- (麻烦)Flip Game (CD1488)
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- flip game
- Flip Game
- Flip Game
- FLIP GAME
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- 快速幂取模
- 安装Apache
- About me 自我介绍网站
- 文件读写的文本方式与二进制方式、文本文件与二进制文件之区别
- 被动的主动性
- (麻烦)Flip Game (CD1488)
- POJ 3157 And Then There Was One
- Quick Linux Server Installation
- 第12周实验报告2
- pthread_cond_timedwait实例代码
- c++ unio 学习总计
- 指针的赋值和内存访问
- Efficient C Tips #6 – Don’t use the ternary operator
- 素数筛选与快速幂取模