24h購物| | PChome| 登入
2013-04-08 08:01:52| 人氣798| 回應0 | 上一篇 | 下一篇

[UVA][dfs] 705 - Slash Maze

推薦 0 收藏 0 轉貼0 訂閱站台


  Slash Maze 

By filling a rectangle with slashes (/) and backslashes ( $backslash$), you can generate nice little mazes. Here is an example:

As you can see, paths in the maze cannot branch, so the whole maze only contains cyclic paths and paths entering somewhere and leaving somewhere else. We are only interested in the cycles. In our example, there are two of them.

Your task is to write a program that counts the cycles and finds the length of the longest one. The length is defined as the number of small squares the cycle consists of (the ones bordered by gray lines in the picture). In this example, the long cycle has length 16 and the short one length 4.

Input 

The input contains several maze descriptions. Each description begins with one line containing two integers w and h ( $1 le w, h le 75$), the width and the height of the maze. The next h lines represent the maze itself, and contain w characters each; all these characters will be either ``/" or ``\".

The input is terminated by a test case beginning with w = h = 0. This case should not be processed.

Output 

For each maze, first output the line ``Maze #n:'', where n is the number of the maze. Then, output the line ``kCycles; the longest has length l.'', where k is the number of cycles in the maze and l the length of the longest of the cycles. If the maze does not contain any cycles, output the line ``There are no cycles.".

Output a blank line after each test case.

Sample Input 

6 4
\//\\/
\///\/
//\\/\
\/\///
3 3
///
\//
\\\
0 0

Sample Output 

Maze #1:
2 Cycles; the longest has length 16.

Maze #2:
There are no cycles.



Miguel A. Revilla
2000-02-09

真的把給定的圖畫出來,分成偶數列與奇數列



   (0100)(0010)(0010)(0100)(0100)(0010)
(1000)(0110)(0011)(1001)(1100)(0110)(0001)
   (1100)(0011)(0011)(1010)(1100)(0011)
(1000)(0110)(0011)(0011)(1001)(0110)(0001)
   (1010)(0011)(0101)(0101)(1010)(0101)
(0010)(0011)(1001)(1100)(0110)(1001)(0100)
   (0101)(0011)(1100)(1010)(0011)(1010)
(1000)(0110)(1001)(0110)(0011)(0011)(0001)
   (1000)(0001)(1000)(0001)(0001)(0001)


分成四個方位 0, 1, 2, 3 分別是右上、左下、右下、左上。
只是很不方便處理,標記1代表這個方位是不可以走的。

由於給定的子圖不是 tree 就是 cycle, 用 dfs 搜索一次吧。



#include <stdio.h>
#include <string.h>
char g[200][200][4] = {};
char used[200][200];
int n, m, cnt, maxlen;
int dfs(int x, int y, int sx, int sy, int dep) {
    if(dep > 3 && x == sx && y == sy) {
        cnt++;
        if(dep-1 > maxlen)
            maxlen = dep-1;
        return 1;
    }
    int lx = 0, rx = 2*m;
    if(x < lx || x > rx)    return 0;
    int ly = (x%2 == 0), ry = n;
    if(y < ly || y > ry)    return 0;
    if(used[x][y])  return 0;
    used[x][y] = 1;
    if(x%2 == 0) {
        int f = 0;
        if(g[x][y][0] == 0)
            f = dfs(x-1, y, sx, sy, dep+1);//0
        if(f)   return 1;
        if(g[x][y][1] == 0)
            f = dfs(x+1, y-1, sx, sy, dep+1);//1
        if(f)   return 1;
        if(g[x][y][2] == 0)
            f = dfs(x+1, y, sx, sy, dep+1);//2
        if(f)   return 1;
        if(g[x][y][3] == 0)
            f = dfs(x-1, y-1, sx, sy, dep+1);//3
        if(f)   return 1;
    } else {
        int f = 0;
        if(g[x][y][0] == 0)
            f = dfs(x-1, y+1, sx, sy, dep+1);//0
        if(f)   return 1;
        if(g[x][y][1] == 0)
            f = dfs(x+1, y, sx, sy, dep+1);//1
        if(f)   return 1;
        if(g[x][y][2] == 0)
            f = dfs(x+1, y+1, sx, sy, dep+1);//2
        if(f)   return 1;
        if(g[x][y][3] == 0)
            f = dfs(x-1, y, sx, sy, dep+1);//3
        if(f)   return 1;
    }
    return 0;
}
int main() {
    char s[200];
    int cases = 0;
    while(scanf("%d %d", &n, &m) == 2) {
        if(n == 0)
            break;
        int i, j, k;
        memset(g, 0, sizeof(g));
        memset(used, 0, sizeof(used));
        for(i = 0; i < m; i++) {
            scanf("%s", s);
            int lx = i<<1, rx = i<<1|1;
            int ly = 1, ry = 0;
            for(j = 0; s[j]; j++) {
                if(s[j] == '\\') {
                    //g[lx][ly] -X- g[rx][ry]
                    //g[rx][ry+1] -X- g[rx+1][lx]
                    g[lx][ly][1] = 1;// can't direct
                    g[rx][ry][0] = 1;
                    g[rx][ry+1][1] = 1;
                    g[rx+1][ly][0] = 1;
                } else {
                    //g[lx][ly] -X- g[rx][ry+1]
                    //g[rx][ry] -X- g[rx+1][ly]
                    g[lx][ly][2] = 1;
                    g[rx][ry+1][3] = 1;
                    g[rx][ry][2] = 1;
                    g[rx+1][ly][3] = 1;
                }
                ly++, ry++;
            }
        }
        /*for(i = 0; i <= m*2; i++) {
            if(i%2 == 0)
                printf("   ");
            for(j = i%2 == 0; j <= n; j++) {
                printf("(%d%d%d%d)", g[i][j][0], g[i][j][1], g[i][j][2], g[i][j][3]);
            }
            puts("");
        }*/
        cnt = 0, maxlen = 0;
        for(i = 0; i <= m*2; i++) {
            for(j = (i%2 == 0); j <= n; j++)
                dfs(i, j, i, j, 1);
        }
        /*for(int i = 0; i <= m*2; i++) {
            if(i%2 == 0)
                printf(" ");
            for(int j = i%2 == 0; j <= n; j++) {
                printf("(%d)", used[i][j]);
            }
            puts("");
        }*/
        printf("Maze #%d:\n", ++cases);
        if(cnt)
            printf("%d Cycles; the longest has length %d.\n", cnt, maxlen);
        else
            puts("There are no cycles.");
        puts("");
    }
    return 0;
}
/*
6 4
\//\\/
\///\/
//\\/\
\/\///
3 3
///
\//
\\\
2 2
/\
\/
0 0
*/

台長: Morris
人氣(798) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][AC自動機] 1254 - Top 10
此分類上一篇:[UVA][dfs] 614 - Mapping the Route

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文