afterward

导航

 

 

 

View Code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
#define MAX_LEN 101
#define is_valid(x, y) ((x)>=0 && (x)<N && (y)>=0 && (y)<M)
int N, M;
char image[MAX_LEN][MAX_LEN];
int visited[MAX_LEN][MAX_LEN];
int hor, vert, diag;

void mark(int i, int j, int dx, int dy, char ch)
{
    while(is_valid(i+dx, j+dy) && image[i+dx][j+dy]==ch)
    {
        visited[i+dx][j+dy] = 1;
        i += dx;
        j += dy;
    }
}

void solve()
{
    int i, j;
    char ch;
    for(i=0; i<N; i++)
    {
        for(j=0; j<M; j++)
        {
            ch = image[i][j];
            if(ch!='.' && !visited[i][j])
            {
                visited[i][j] = 1;
                switch(ch)
                {
                case '-':
                    ++hor;
                    mark(i, j, 0, -1, ch);
                    mark(i, j, 0, 1, ch);
                    break;
                case '|':
                    ++vert;
                    mark(i, j, -1, 0, ch);
                    mark(i, j, 1, 0, ch);
                    break;
                case '\\':
                    ++diag;
                    mark(i, j, 1, 1, ch);
                    mark(i, j, -1, -1, ch);
                    break;
                case '/':
                    ++diag;
                    mark(i, j, 1, -1, ch);
                    mark(i, j, -1, 1, ch);
                    break;
                }
            }
        }
    }
}

int main(int argc, char **argv)
{
    int i, tests;
    scanf("%d", &tests);
    while(tests--)
    {
        scanf("%d %d", &N, &M);
        for(i=0; i<N; i++)
            scanf("%s", image[i]);
        memset(visited, 0, sizeof(visited));
        hor = vert = diag = 0;
        solve();
        if(hor+vert+diag == 1)
            printf("CORRECT\n");
        else
            printf("INCORRECT\n");
    }
}

找出一刀切得痕迹,感觉这个代码思路清晰很到位!

每找到一种切法都按这种切法一直走下去,并标记。当切法多于一种或不只是一刀切得时候incorrect.

 

 

poj  3561 简单搜索
Pseudographical recognizer
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 1672  Accepted: 713

Description

Let us define a pseudographical image to be a rectangular matrix of the characters ’.’, ’?’, ’|’, ’\’, and ’/’.

The character ’.’ denotes empty space on the image. A horizontal line segment is given as a set of ’-’ characters in adjacent cells in the same row of the matrix. A vertical line segment is given as a set of ’|’ characters in adjacent cells in the same column of the matrix. Similarly, a diagonal line segment is given as a set of ’/’ or ’\’ characters in adjacent cells in the same diagonal of the matrix. Of course, a line segment going from Northwest to Southeast has to be given using the ’\’ characters and a line segment going from Southwest to Northeast using the ’/’ characters.

Write a program that, given a pseudographical image, determines if it contains exactly one line segment – horizontal, vertical, or diagonal.

Input

The input contains several test cases. The first line contains the number of test cases T (1 ≤ T ≤ 100). Next follow the descriptions of each test case. The first line of the test case description contains two integers N and M (1 ≤ N, M ≤ 10), the number of rows and columns of the matrix, respectively. Each of the following N lines of the description contains exactly M symbols ‘.’, ‘?’, ‘|’, ‘\’, or ‘/’.
Output

The output consists of T lines, one line per each test case. This line should contain the word CORRECT if the input image contains exactly one line segment, or the word INCORRECT otherwise.

Sample Input

5
5 5
.....
\....
.\...
..\..
.....
3 3
/..
./.
../
3 6
.|....
.|.---
.|....
3 3
...
...
...
1 1
/
Sample Output

CORRECT
INCORRECT
INCORRECT
INCORRECT
CORRECT

posted on 2012-08-05 10:37  afterward  阅读(330)  评论(0编辑  收藏  举报