Kattis - flowfree Flow Free 多个两点的DFS

grid of cells

grid of cells

Flow Free is a puzzle that is played on a 2D grid of cells, with some cells marked as endpoints of certain colors and the rest of cells being blank.

To solve the puzzle, you have to connect each pair of colored endpoints with a path, following these rules:

  • there is a path connecting two points with the same color, and that path also has that color
  • item all cells in the grid are used and each cell belongs to exactly one path (of the same color as the endpoints of the path)

The rules imply that different colored paths cannot intersect.

The path is defined as a connected series of segments where each segment connects two neighbouring cells. Two cells are neighbours if they share a side (so all segments are either horizontal or vertical). By these definitions and rules above, each colored cell will be an endpoint of exactly one segment and each blank cell will be an endpoint of exactly two segments.

In this problem we will consider only the 4 × 4 puzzle, with 3 or 4 pairs of colored endpoints given.

Your task is to determine if a given puzzle is solvable or not.

Input

The input consists of 4 lines, each line containing 4 characters.
Each character is from the set {R, G, B, Y, W} where W denotes the blank cells and the other characters denote endpoints with the specified color. You are guaranteed that there will be exactly 3 or 4 pairs of colored cells. If there are 3 colors in the grid, Y will be omitted.

Output

扫描二维码关注公众号,回复: 2757875 查看本文章

On a single line output either "solvable" or "not solvable" (without the quotes).

Sample Input

RGBW
WWWW
RGBY
YWWW

RGBW
WWWW
RGBW
YWWY

Sample Output

solvable
not solvable

题意:4*4的面板,给出一 一对应的非白色色块,然后判断所有一 一对应的色块是否可以在寻找彼此的时候可以遍历4*4的面板

思路:DFS 毋庸置疑,但是具体要怎么想实现,最后我想的是先确定一个非白色的色块,然后找他与他匹配的,找到后再寻找另外一个非白色,再寻找。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
const int MAXN=1e6+5;
const int P=99991;
const int N = 5;
typedef long long ll;
using namespace std;
char s[N][N];
int vis[N][N];
int dir[4][2]= {1,0,-1,0,0,1,0,-1};
int num,flag=0;
int judge(int x,int y)
{
    if(0<=x&&x<4&&0<=y&&y<4)
        return 0;
    return 1;
}
void DFS(int x,int y,char ch)
{
    if(s[x][y]!='W'&&s[x][y]!=ch)
        return ;
    if(judge(x,y)||vis[x][y])  //判断是否访问过
        return ;
    num++;                    //使用该节点
    vis[x][y]=1;
    //printf("%d\n",num);
    if(num>=16)
    {
        flag=1;
        return ;
    }
    for(int i=0; i<4; i++)
    {

        int ax=x+dir[i][0];
        int ay=y+dir[i][1];
        if(!judge(ax,ay)&&!vis[ax][ay])
        {
            if(s[ax][ay]==ch)   //找到了对应点
            {
                num++;
                vis[ax][ay]=1;
                if(num==16){
                    flag=1;
                    return ;
                }
                for(int ii=0; ii<4; ii++)
                    for(int jj=0; jj<4; jj++)
                        if(s[ii][jj]!='W'&&!vis[ii][jj]&&s[ii][jj]!=ch)
                        {
                            int a=ii;
                            int b=jj;
                            char c=s[ii][jj];
                            DFS(a,b,c);
                        }
                num--;
                vis[ax][ay]=0;
            }
            else{
                if(s[ax][ay]!='W')
                    continue;
                else
                    DFS(ax,ay,ch);
            }
        }
    }
    num--;
    vis[x][y]=0;
}
int main()
{
    for(int i=0; i<4; i++)
    {
        scanf("%s",s[i]);
        getchar();
    }
    //for(int i=0;i<4;i++)
     // printf("%s\n",s[i]);
    int x,y,f=0;
    char ch;
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
            if(s[i][j]!='W')
            {
                x=i;y=j;ch=s[i][j];
                f=1;break;
            }
        if(f==1)
            break;
    }
    memset(vis,0,sizeof(vis));
    num=flag=0;
    DFS(x,y,ch);
    //printf("%d %d\n",num,flag);
    if(flag)
        printf("solvable\n");
    else
        printf("not solvable\n");

}

猜你喜欢

转载自blog.csdn.net/deepseazbw/article/details/81281628