Sicily 1056. Obstructed Rook Circ

1056. Obstructed Rook Circ

Constraints

Time Limit: 10 secs, Memory Limit: 32 MB , Special Judge

Description

A board is a rectangular array of squares such as on a chessboard, with possibly some squares blocked off. A rook tour of a board is a path that visits each empty square of the board exactly once, moving at each step to an empty adjacent square (North, South, East or West but not diagonally). A rook tour is a rook circuit if it starts and ends on the same square. In the figures below, let the + symbol be the rook, and the X symbol be an obstruction. The following are descriptions of each figure: (a) is a board with no rook circuit, (b) and (c) give distinct rook circuits of the same board and (d) gives the unique (up to direction) rook circuit of another board.

Write a program, which takes as input the description of a board and either finds a rook circuit or determines that there is no rook circuit.

Input

Input consists of a sequence of board descriptions and starting points. The first line of the input is

Nrows Ncols Nblocks StartX StartY

where Nrows is the number of rows in the rectangular array, Ncols is the number of columns in the rectangular array, Nblocks is the number of blocked off squares on the board and (StartX, StartY) is the position on the board where the path is to start (and end). StartX and StartY are 0 based (StartX ranges from 0 to Ncols - 1). Following the first line there are Nblocks lines giving the coordinates of the blocked off squares, one per line. The coordinates of these points are 0 based and are of the form X Y. The final line of each board description is a blank line.

The last line of the input is line of 5 zeroes.

Output

If there is no rook circuit for the corresponding board, the output is a line "NO SOLUTION" followed by a blank line. Otherwise, the output is a sequence of the letters N, S, E, W giving the moves from the starting point to traverse a rook circuit and return to the starting point. (N indicates moving to the previous row, S moving to the next row, E moving to the next column and W moving to the previous column.) If more than 40 moves are required, the moves will be output 40 to a line (except possibly for the last line). The move output is to be followed by a blank line.

Some Facts:

1. The Parity Principle. If we checker the squares of the array black and white (white if (x+y) is even, black if (x+y) is odd), each unit step in a rook circuit must go from a white square to a black square and vice versa. Thus any rook circuit must have the same number of white and black squares. Board (a) above has 8 white and 6 black unblocked squares so cannot have a rook circuit.

2. The Two Neighbor Principle. If a square has only two neighbors, then it must be visited via those neighbors. When a circuit gets to one of the neighbors, it must pass next to the two -neighbor square and then to the other neighbor. In board (d), the (0, 0), (3, 0), (0, 2), (1,3), (2,3), (3,3) and (3,2) squares each have two neighbors. Thus the path is forced to include (1,0)-(0,0)-(0,1)-(0,2)-(1,2)-(1,3)-(2,3)-(3,3)-(3,2)- (3,1)-(3,0)-(2,0) in either order.

3. The Cul-de-Sac Principle. Never draw segments that leave a square with only one exit.

4. The Early Closing Principle. Never close a circuit until all squares have been visited.

The first four boards and solutions correspond to the pictures in the figures above. Note that the same board may have several rook circuits. Your program need only find any one (correct) rook circuit.

Sample Input

4 4 2 0 0
1 2
3 0
4 4 0 2 2
4 4 0 0 0
4 4 2 1 2
0 3
2 2
8 8 0 0 0
0 0 0 0 0

Sample Output

NO SOLUTION

NENWWWSESWSEEENW

EEESWWSEESWWWNNN

WNNESENESSSWWN

EEEEEEESWWWWWWSEEEEEESWWWWWWSEEEEEESWWWW

WWSEEEEEESWWWWWWWNNNNNNN

// Problem#: 1056
// Submission#: 3458950
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <string.h>
#include <math.h>
#include <list>
#include <set>
using namespace std;

const int MAX_W = 100;

int W, H, N, Si, Sj;
char G[MAX_W][MAX_W];
int StepSum;
char step[MAX_W * MAX_W];
bool finish;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

struct Next {
    int i, j, p;
    Next(int ii = 0, int jj = 0, int pp = 0) {
        i = ii;
        j = jj;
        p = pp;
    }
};

bool judge() {
    int w, b;
    w = b = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (G[i][j] == ' ') {
                if ((i + j) & 1) b++;
                else w++;
            }
        }
    }
    return b == w;
}

bool isIn(int i, int j) {
    return 0 <= i && i < H && 0 <= j && j < W;
}

int calPossibility(int i, int j) {
    int p = 0;
    for (int k = 0; k < 4; k++) {
        if (isIn(i + dir[k][0], j + dir[k][1]) && (G[i + dir[k][0]][j + dir[k][1]] == ' ' || (i + dir[k][0] == Si && j + dir[k][1] == Sj))) p++;
    }
    return p;
}

bool cmp(const Next & n1, const Next & n2) {
    return n1.p < n2.p;
}

void dfs(int nowStep, int lastI, int lastJ) {
    if (nowStep == StepSum) {
        bool canArrive = false;
        for (int i = 0;  i < 4; i++) {
            if (isIn(lastI + dir[i][0], lastJ + dir[i][1]) && lastI + dir[i][0] == Si && lastJ + dir[i][1] == Sj) {
                if (dir[i][0] == -1) step[nowStep] = 'N';
                if (dir[i][0] == 1) step[nowStep] = 'S';
                if (dir[i][1] == 1) step[nowStep] = 'E';
                if (dir[i][1] == -1) step[nowStep] = 'W';
                finish = true;
                break;
            }
        }
        return;
    }
    Next next[4];
    int num = 0;
    for (int i = 0; i < 4; i++) {
        int nexti = lastI + dir[i][0];
        int nextj = lastJ + dir[i][1];
        if (isIn(nexti, nextj) && G[nexti][nextj] == ' ') {
            next[num++] = Next(nexti, nextj, calPossibility(nexti, nextj));
        }
    }
    sort(next, next + num, cmp);
    bool mustGo = false;
    for (int i = 0; i < num; i++) {
        if (next[i].p == 0) return;
        if (next[i].p == 1) mustGo = true;
        G[next[i].i][next[i].j] = '.';
        if (next[i].i < lastI) step[nowStep] = 'N';
        if (next[i].i > lastI) step[nowStep] = 'S';
        if (next[i].j > lastJ) step[nowStep] = 'E';
        if (next[i].j < lastJ) step[nowStep] = 'W';
        dfs(nowStep + 1, next[i].i, next[i].j);
        if (!finish) {
            G[next[i].i][next[i].j] = ' ';
            if (mustGo) return;
        } else return;
    }
}

void specialSolve() {
    int all = H * W, now = 0;
    int nowi = Si, nowj = Sj;
    finish = true;
    if (W % 2 == 0) {  // 如果列数是偶数
        while (now < all) {
            if (nowi == 0) {  // 如果在第一行
                if (nowj == W - 1) {  // 如果在第一行最右向南
                    step[now] = 'S';
                    nowi++;
                } else {  // 否则都向东
                    step[now] = 'E';
                    nowj++;
                }
            } else {  // 如果不在第一行
                if (nowj % 2 == 0) {  // 如果在偶数列
                    if (nowi == 1 && nowj != 0) {  // 如果在第二行并且不是第一列的第二行向西
                        step[now] = 'W';
                        nowj--;
                    } else {  // 如果不在第二行,或者在第一列的第二行向北
                        step[now] = 'N';
                        nowi--;
                    }
                } else {  // 如果在奇数列
                    if (nowi == H - 1) {  // 如果在最后一行向西
                        step[now] = 'W';
                        nowj--;
                    } else {  // 否则都向南
                        step[now] = 'S';
                        nowi++;
                    }
                }
            }
            now++;
        }
    } else {
        while (now < all) {
            if (nowj == 0) {
                if (nowi == 0) {
                    step[now] = 'E';
                    nowj++;
                } else {
                    step[now] = 'N';
                    nowi--;
                }
            } else {
                if (nowi % 2 == 1) {
                    if (nowj == 1 && nowi != H - 1) {
                        step[now] = 'S';
                        nowi++;
                    } else {
                        step[now] = 'W';
                        nowj--;
                    }
                } else {
                    if (nowj == W - 1) {
                        step[now] = 'S';
                        nowi++;
                    } else {
                        step[now] = 'E';
                        nowj++;
                    }
                }
            }
            now++;
        }
    }
}

void output() {
    if (!finish) {
        cout << "NO SOLUTION" << endl << endl;
        return;
    }
    for (int i = 0, counter = 0; i <= StepSum; i++, counter++) {
        if (counter == 40) {
            counter = 0;
            cout << endl;
        }
        cout << step[i];
    }
    cout << endl << endl;
}

int main() {

    std::ios::sync_with_stdio(false);

    while (1) {
        cin >> H >> W >> N >> Sj >> Si;
        if (!H) break;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                G[i][j] = ' ';
            }
        }
        for (int i = 0; i < N; i++) {
            int ti, tj;
            cin >> tj >> ti;
            G[ti][tj] = 'X';
        }
        finish = false;
        StepSum = W * H - N - 1;
        if (H < 2 || W < 2 || !judge()) {
            output();
            continue;
        }
        if (N == 0) {
            specialSolve();
            output();
            continue;
        }
        G[Si][Sj] = '.';
        dfs(0, Si, Sj);
        output();
    }

    return 0;
}                                 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值