DFS 训练题

来源:互联网 发布:linux中tar命令 编辑:程序博客网 时间:2024/05/29 14:40

C - Flip Game
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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: 
  1. Choose any one of the 16 pieces. 
  2. 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 the word "Impossible" (without quotes).

Sample Input

bwwbbbwbbwwbbwww

Sample Output

4

题意:翻动其中的一个棋子,旁边的棋子也会跟着改变颜色,目标是全盘白或者是全盘黑~

WAY:DFS+枚举...DFS弄了很才弄懂TT...弄懂之后发现没什么好说的了~

MY CODE:

#include<stdio.h>#include<iostream>#include<algorithm>using namespace std;const int M=32;bool map2[5][5];int s,i,j,cur,ans;bool finish(){    for(i=0;i<4;i++)        for(j=0;j<4;j++)        {            if(map2[i][j]!=map2[0][0])                return 0;        }    return 1;}void change(int cur){    int x,y;    x=cur/4;    y=cur%4;    map2[x][y]=!map2[x][y];    map2[x+1][y]=!map2[x+1][y];    if(x-1>=0)    {map2[x-1][y]=!map2[x-1][y];}    if(y-1>=0)    {map2[x][y-1]=!map2[x][y-1];}    map2[x][y+1]=!map2[x][y+1];}void dfs(int cur,int s){    if(finish()) {ans=min(ans,s);return ;}    if(cur>15) return ;    dfs(cur+1,s);    change(cur);    dfs(cur+1,s+1);    change(cur);}int main(){    char map;    for(i=0;i<4;i++)    {        for(j=0;j<4;j++)        {             scanf("%c",&map);             if(map=='b')map2[i][j]=1;             if(map=='w')map2[i][j]=0;        }        getchar();    }    ans=M;    dfs(0,0);    if(ans!=M)printf("%d\n",ans);    else printf("Impossible\n");    return 0;}


D - The Pilots Brothers' refrigerator
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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

61 11 31 44 14 34 4

题意:改变其中一个纽扣的状态,所在的那行那列也会改变状态,目标是全部都开,即' - '~

WAY:和上题极像极像,也是枚举+DFS....但是在储存纽扣位置上折腾了很久..应该先储存在临时数组上,如果完成才转换在用于输出的数组上~

MY CODE:

#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;const int M=32;bool map2[5][5];int x[20],y[20];//临时int ansx[20],ansy[20];int s,i,j,cur,ans,k=0;bool finish(){    for(i=0;i<4;i++)        for(j=0;j<4;j++)        {            if(map2[i][j]!=1)                return 0;        }    return 1;}void change(int cur){    int x,y;    x=cur/4;    y=cur%4;     for(int i=0;i<4;i++)    {        map2[x][i]=!map2[x][i];        map2[i][y]=!map2[i][y];    }    map2[x][y]=!map2[x][y];}void dfs(int cur,int s){    if(finish())    {        if(ans>s)        {            ans=s;            for(i=1;i<=ans;i++)            {                ansx[i]=x[i];                ansy[i]=y[i];            }        }        return ;    }    if(cur>15)return ;    dfs(cur+1,s);    change(cur);    x[s+1]=cur/4+1;    y[s+1]=cur%4+1;    dfs(cur+1,s+1);    change(cur);}int main(){    char map;    for(i=0;i<4;i++)    {        for(j=0;j<4;j++)        {             scanf("%c",&map);             if(map=='-')map2[i][j]=1;             if(map=='+')map2[i][j]=0;        }        getchar();    }    ans=M;    dfs(0,0);    printf("%d\n",ans);    for(i=1;i<=ans;i++)    {        printf("%d %d\n",ansx[i],ansy[i]);    }    return 0;}

附上: 神牛解法

参考高手的高效解法:> 证明:要使一个为'+'的符号变为'-',必须其相应的行和列的操作数为奇数;可以证明,如果'+'位置对应的行和列上每一个位置都进行一次操作,则整个图只有这一'+'位置的符号改变,其余都不会改变.> 设置一个4*4的整型数组,初值为零,用于记录每个点的操作数,那么在每个'+'上的行和列的的位置都加1,得到结果模2(因为一个点进行偶数次操作的效果和没进行操作一样,这就是楼上说的取反的原理),然后计算整型数组中一的> 个数即为操作数,一的位置为要操作的位置(其他原来操作数为偶数的因为操作并不发生效果,因此不进行操作)
#include <iostream>using namespace std;bool mark[4][4];char s[4][4];int main(){    int i,j,k;    int ci[16],cj[16];    int nas = 0;    memset(mark,0,sizeof(mark));for(i = 0;i < 4;i++)cin >> s[i];    for(i = 0;i < 4;i++)        for(j = 0;j < 4;j++)        {            char c = s[i][j];            if(c == '+')            {                mark[i][j] = !mark[i][j];                for(k = 0;k < 4;k++)                {                    mark[i][k] = !mark[i][k];                    mark[k][j] = !mark[k][j];                }            }        }    for(i = 0;i < 4;i++)        for(j = 0;j < 4;j++)            if(mark[i][j] == true)            {                ci[nas] = i + 1;                cj[nas] = j + 1;                nas ++;            }    printf("%d\n",nas);    for(i = 0;i < nas;i++)    {        printf("%d %d\n",ci[i],cj[i]);    }    return 0;}


0 0