UVa11700 - Pipes

5 篇文章 0 订阅

Problem B: Pipes

After writing a solver for the "moveable maze" game last week, you have grown tired of it. After all, you already know the optimal solution. To entertain yourself, you find another puzzle game called "Pipes", and play that for a while. On one puzzle, you have not been able to find a solution by hand, and you think that there is no solution. You decide to write a program to tell you whether this is the case.

The game is played on a grid with R rows and C columns. Each square of the grid contains a black dot in the centre and black lines in the direction of some, none, or all of its north, east, south, and west neighbouring squares, with the following restriction: if two opposite directions both have lines, then at least one of the other two directions has a line as well. In other words, it is forbidden for a square to consist of a straight line.

The objective of the game is to rotate each square, as many times as you like, such that for each square, if it has a line going in a compass direction (that is, north, east, south, or west), then it has a neighbour in that compass direction and that neighbour has a line going in the opposite compass direction. In other words, each edge in the grid should either have a line on both sides or neither side.

Your task is to determine whether a given grid has a solution.

Input Specification

The input consists of several test cases.

The first line of each test case contains the two integers R and C, separated by spaces, with 1 <= R,C <= 12.

The following R lines of input each contain one row of the grid, from north to south. Each of these lines contains exactly C strings of letters, separated by spaces, that correspond to squares of the grid, from west to east. Their format is as follows:

  • If the string is the single character x, then the square does not contain a line to any of its neighbours.
  • Otherwise, the string contains some of the characters NESW, which indicate that a black line extends from this square's centre in the direction of its north, east, south, or west neighbour, respectively. No character will appear in the string more than once.

Input is terminated by a line containing 0 0. These zeros are not a test case and should not be processed.

Sample Input

3 3
NW NW x
NES NESW W
E W x
2 2
ES x
x N
0 0

Output Specification

For each test case, output  SOLVABLE  if there is a solution to the puzzle, and  UNSOLVABLE  otherwise.

Output for Sample Input

SOLVABLE
UNSOLVABLE
dp[row][mask]  
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
int dp[12][1<<12];
map<char,int> mma;
int mat[12][12];
string st;
int n,m;
void init(){
    memset(dp,-1,sizeof dp);
}
int roat(int Mask){
    int last = Mask&(1<<3);
    if(last) last = 1;
    int nowmask = ((Mask<<1)&15)|last;
    return nowmask;
}
void dfs(int row,int col,int lastmask,int right,int dmask){
    if(col==m){
        if(right==0)
            dp[row][dmask] = 1;
        return;
    }
    int tk = mat[row][col],top = (lastmask>>col)&1;

    for(int i = 0; i < 4; i++){
            int left = (tk>>1)&1,down = (tk>>2)&1,tf = (tk>>3)&1,tt = tk&1;
            if(tt==top&&right==tf)
                dfs(row,col+1,lastmask,left,dmask|(down<<col));
            tk = roat(tk);
    }
    return;
}

int main(){

    mma['N'] = 1;mma['E'] = 2;mma['S'] = 4;mma['W'] = 8;mma['x'] = 0;
    while(cin >> n >> m &&n+m){
        init();
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++){
                cin >> st;
                int as = 0;
                for(int k = 0; k < st.size(); k++)  as += mma[st[k]];

                mat[i][j] = as;
            }
        dfs(0,0,0,0,0);
        bool flag = 0;
        for(int i = 1; i < n; i++){
            for(int mask = 0; mask <(1<<m); mask++)
                if(dp[i-1][mask]==1){
                    dfs(i,0,mask,0,0);
                }

        }
        if(dp[n-1][0]==1) cout<<"SOLVABLE"<<endl;
        else cout<<"UNSOLVABLE"<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值