终于自己完整的敲完。。。。。
好多帖子都说这个是用枚举和深搜,由于最近ACM队开始研究数据结构。。这个解决方法不是我想出来的。代码是看了别人的以后再自己敲的。原谅我这个菜鸟实在没有想出方法。。。。
1753的题意就是每次翻开4*4棋盘中的一个棋子,然后与这个棋子相邻的棋子也翻开,棋子是黑白两面的。问最少翻几下可以让一面颜色全部一样,,原题如下:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14288 | Accepted: 6140 |
Description
- 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
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
我把自己的理解都放在程序注释上了,,,程序如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
bool map[4][4];
int ans=100;
void init()
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
char ch;
cin>>ch;
if(ch=='b') map[i][j]=true;
else map[i][j]=false;
}
getchar();
}
}
void operate(int x,int y)
{
if(x<0||y<0||x>3||y>3)
return ;
map[x][y]=!map[x][y];
}
void turn(int pos)
{
int x = pos / 4;//这个地方很不错,直接用pos/4作为x ;
int y = pos % 4;//pos%4作为y,让程序更简单,也容易想;
operate(x,y);
operate(x-1,y);
operate(x,y-1);
operate(x+1,y);
operate(x,y+1);
}
bool finished() //判断是否可以转换成全部是一面颜色;
{
int tot=0;
int i;
for(i=0;i<16;i++)
tot+=map[i/4][i%4];
return ((tot%16)==0);//是,则返回1;不是,则返回0;一句搞定。
}
void dfs(int pos , int step)
{
if(finished())
{
if(ans>step)
ans=step;
return ;
}
if(pos>=16)
return ;
dfs(pos+1,step); //这一块要知道为什么有step和step+1;
turn(pos); //因为有时候这一步step不可取,所以不要+1,有的则要。
dfs(pos+1,step+1); //看了半天才知道是这个意思。。。
turn(pos);
}
int main()
{
init();
dfs(0,0);
if(ans==100)
cout<<"Impossible"<<endl;
else cout<<ans<<endl;
return 0;
}
而2965的思想基本一样,题目如下:题意就是让每次选中的+或者-,那么这个符号所在的位置的行和列都要变为与其相反的,知道这一面符号全一样。求最少的次数,并且把方法打印出来。
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 9904 | Accepted: 3614 | Special Judge |
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+-- ---- ---- -+--
Sample Output
6 1 1 1 3 1 4 4 1 4 3 4 4
要是只是算出次数,和上面的基本是一样的。打印出来只要加一个output的函数,用ans记录每一步(一共是16步)是否可可以成立,然后,完成任务是,就逐个打印出来。。。。代码如下,(参考别人的后自己写的,唉o(︶︿︶)o 唉)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
bool map[4][4];
int ans[16];
void init()
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
char c;
cin>>c;
if(c== '-') map[i][j]=true;
else map[i][j]=false;
}
getchar();
}
}
void operate(int x,int y)
{
if(x<0||y<0||x>3||y>3)
return ;
map[x][y]=!map[x][y];
}
void turn(int pos)
{
ans[pos]=!ans[pos]; //这个不能少,记录每次反翻动情况,要是操作两次,则和没有操作一样。
int x=pos/4;
int y=pos%4;
int i;
operate(x,y);
for(i=1;i<4;i++)
{
operate(x+i,y);
operate(x,y+i);
operate(x-i,y);
operate(x,y-i);
}
}
bool finished()
{
int i,j;
int tot=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
tot+=map[i][j];
}
return(tot==16);
}
void output()//输出函数
{
int i;
for(i=0;i<16;i++)
{
if(ans[i])
printf("%d %d\n",i/4+1,i%4+1);
}
}
void dfs(int pos,int step)
{
if(finished())
{
cout<<step<<endl;
output();
exit(0);
return ;
}
if(pos>=16)
return;
dfs(pos+1,step);
turn(pos);
dfs(pos+1,step+1);
turn(pos);
}
int main()
{
init();
dfs(0,0);
return 0;
}
继续加油吧,这个暑假好好搞!!!!!!