[kuangbin带你飞]专题一 简单搜索 -D

来源:互联网 发布:网络监控存储 编辑:程序博客网 时间:2024/05/18 10:16

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 × 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”.

Input
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

Output
Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

Sample Input
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1

Sample Output
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

爆炸

#include"iostream"#include"string.h"using namespace std;int m,n;int ai[20][20];bool change[20][20];int fx[5][2]={1,0,-1,0,0,1,0,-1,0,0};bool jj=false;int mm[20][20];int mmin=1e9;int ji=0;bool check(int a,int b){    if(a>=0&&a<m&&b>=0&&b<n)        return true;    return false;}void dfs(int cur,int bi[20][20],int num){    if(cur==n)    {        int ci[20][20];        for(int i=0;i<m;i++)        {            for(int j=0;j<n;j++)            {                ci[i][j]=bi[i][j];            }        }        for(int i=0;i<m-1;i++)        {            for(int j=0;j<n;j++)            {                if(ci[i][j])                {                    num++;                    change[i+1][j]=true;                    for(int k=0;k<5;k++)                    {                        int a=i+1+fx[k][0];                        int b=j+fx[k][1];                        if(check(a,b))                        {                        //  cout<<a<<" "<<b<<endl;                            ci[a][b]=1-ci[a][b];                        }                    }                //  cout<<endl;                }            }        }        //cout<<num<<endl<<endl;        bool sign=true;        for(int i=0;i<n;i++)        {            if(ci[m-1][i])            {                sign=false;                break;            }        }        if(sign&&num<mmin)        {            //cout<<num<<endl;            jj=true;            mmin=num;            for(int i=0;i<m;i++)            {                for(int j=0;j<n;j++)                {                    mm[i][j]=change[i][j];                }            }        }        return;    }    for(int i=0;i<m;i++)        {            for(int j=0;j<n;j++)            {                cout<<bi[i][j]<<" ";            }            cout<<endl;        }        cout<<endl;    change[0][cur]=false;    dfs(cur+1,bi,num);    change[0][cur]=true;    num++;    for(int i=0;i<5;i++)    {        int a=0+fx[i][0];        int b=cur+fx[i][1];        if(check(a,b))        {            bi[a][b]^=1;        }    }    dfs(cur+1,bi,num);}int main(){    cin>>m>>n;    for(int i=0;i<m;i++)    {        for(int j=0;j<n;j++)        {            cin>>ai[i][j];        }    }    dfs(0,ai,0);    if(!jj)    cout<<"IMPOSSIBLE"<<endl;    else    {        for(int i=0;i<m;i++)        {            for(int j=0;j<n-1;j++)            {                cout<<mm[i][j]<<" ";            }            cout<<mm[i][n-1]<<endl;        }    }    //cout<<mmin<<endl;    return 0;}

下面这个是别人的代码,改日来参考

#include<stdio.h>#include<string.h>int t[30][30], tem[30][30], m[30][30];int M,N,dir[5][2] = { 0,0,1,0,0,1,-1,0,0,-1 };int get(int x, int y)//获得x,y点的颜色{    int c = t[x][y];    for (int i = 0; i < 5; i++)    {        int x1 = x + dir[i][0], y1 = y + dir[i][1];        c += tem[x1][y1];    }    return c % 2;}int cal()//计算2行及之后的,有解返回翻点数,无解返回-1{    for (int i = 2; i <= M; i++)        for (int j = 1; j <= N; j++)            if (get(i - 1, j) == 1)                tem[i][j] = 1;    for (int i = 1; i <= N; i++)        if (get(M, i))return -1;    int res = 0;    for (int i = 1; i <= M; i++)        for (int j = 1; j <= N; j++)            res += tem[i][j];    return res;}int main(){    int min = -1;//次数>0可以这样初始化    scanf("%d%d", &M, &N);    for (int i = 1; i <= M; i++)        for (int j = 1; j <= N; j++)            scanf("%d", &t[i][j]);    for (int i = 0; i < (1 << N); i++)//枚举第一行    {        memset(tem, 0, sizeof(tem));        for (int j = 1; j <= N; j++)            tem[1][j] = (i >> (j - 1)) & 1;        int num = cal();        if (num >= 0 && (min<0 || min>num))        {            min = num;            memcpy(m, tem, sizeof(tem));        }    }    if (min == -1)printf("IMPOSSIBLE\n");    else    {        for (int i = 1; i <= M; i++)            for (int j = 1; j <= N; j++)                printf("%d%c", m[i][j], j == N ? '\n' : ' ');    }}
1 1