poj1753和2965題(棋盤上的棋子翻轉最後達到同一狀態)

想想還是得多刷點題。有兩道類似的題,都用了深搜和枚舉,分別是POJ上的1753題和2965題,先粘貼如下,這個代碼是參考別人的代碼。思路很清晰,借鑑過來。
Flip Game(1753題)
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21024 Accepted: 9108
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
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 theword”Impossible” (without quotes).
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
這個題目的意思是:有一個四乘四的棋盤,每個棋子有兩種狀態,非黑即白,翻轉一個棋子,它自身以及它上下左右的棋子都會發生翻轉,問最少翻轉幾次(在可能的情況下),使得棋盤上的顏色統一。代碼和註釋如下。

#include<stdio.h>//1753棋盤翻轉
#include<iostream>
using namespace std;
int chess[4][4];
int c=33;
void build()//將棋盤的顏色以標記化
{
    char c;
    int i,j;
    for(i=0;i<4;i++)
    for(j=0;j<4;j++)
    {
        cin>>c;//此處採用c++的輸入,之前用C語言一直出錯,原因在於C語言的輸入中會把換行也當成是一個字符
        if(c=='w')
        chess[i][j]=0;
        else
        chess[i][j]=1;
    }
}
void turn(int x,int y)//翻轉
{
     if(x>=0&&x<=3&&y>=0&&y<=3)//如果是在棋盤的四周,那麼翻轉的時候不一定上下左右都存在
     chess[x][y]=!chess[x][y];
}
void flip(int s)//一個棋子變化,周圍四個都要變化
{
    int i=s/4;//行
    int j=s%4;//列
    turn(i,j);
    turn(i+1,j);
    turn(i,j+1);
    turn(i-1,j);
    turn(i,j-1);
}
int complete()//判斷棋盤是否變成同一的顏色
{
    int i,j,s1=0;
    for(i=0;i<4;i++)
       for(j=0;j<4;j++)
          s1+=chess[i][j];
    if(s1%16)
      return 0;
    else
      return 1;
}
void dfs(int s,int b)//進行深搜.s代表當前的方格,b代表翻轉的方格數
{
     if(complete())//如果是同一顏色,找到最終狀態
     {
        if(c>b)
           c=b;
        return;
     }
     if(s>=16)//如果遍歷完
        return;
    dfs(s+1,b);//不翻轉,一直往下進行搜索
    flip(s);//回溯到上一層,翻轉此棋子
    dfs(s+1,b+1);//再進行往下搜索的過程
    flip(s);//回到上一層,把棋子的狀態恢復
}
int main()
{
    build();//將棋盤的顏色以標記化
    dfs(0,0);
    if(c==33)//由於翻轉次數最多爲4*4*2=32次
      printf("Impossible\n");
    else
      printf("%d\n",c);
     return 0;
}
                    2965題

這個題大致同前一題相似,只是翻轉的過程和最後的狀態不同,翻轉時該點所在的整行整列都會發生變化,同時最後的狀態是棋盤都是“-”的狀態,而且多了要記錄下翻轉的點的過程。同樣採取搜索和枚舉,每個點有翻轉和不翻轉兩種狀態。代碼和註釋如下。

#include<stdio.h>
#include<iostream>
using namespace std;
int location[4][4];
int cnt=33;
int x[16],y[16];//臨時的把這些點都記錄下來
int ansx[16],ansy[16];//最終的結果
void build()
{
    char c;
    int i,j;
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
          {
            cin>>c;
            if(c=='+')
                location[i][j]=0;
            else
                location[i][j]=1;
          }
}
int success()//判斷是否達到了最後的狀態
{
    int i,j;
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
         {
             if(location[i][j]==0)
                return 0;
         }
    return 1;
}
void switching(int s)
{
    int a=s/4;
    int b=s%4;
    int i,j;
    for(j=0;j<4;j++)
    {
        location[a][j]=!location[a][j];
    }
    for(i=0;i<4;i++)
    {
        location[i][b]=!location[i][b];
    }
    location[a][b]=!location[a][b];//該點在前面的過程中被翻轉兩次,相當翻轉回來,再翻轉一次
}
void dfs(int s,int b)
{
    int i;
    if(success())
        {
            if(cnt>b)
            {
               cnt=b;
               for(i=0;i<cnt;i++)
             {
                 ansx[i]=x[i];//如果此次搜索成功了,那麼在此過程中翻轉的點就是問題的解
                 ansy[i]=y[i];
             }
            }
           return;
        }
    if(s>=16)
        return ;
    dfs(s+1,b);
    switching(s);
    x[b]=s/4;//記錄所有翻轉的點
    y[b]=s%4;
    dfs(s+1,b+1);
    switching(s);
}
int main()
{
    build();
    dfs(0,0);
    printf("%d\n",cnt);
    for(int i=0;i<cnt;i++)
        printf("%d %d\n",ansx[i]+1,ansy[i]+1);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章