The Ninth Hunan Collegiate Programming Contest (2013) Problem C

Problem C

Character Recognition?

Write a program that recognizes characters. Don't worry, because you only need to recognize three digits: 1, 2 and 3. Here they are:

.*.  ***  ***
.*.  ..*  ..*
.*.  ***  ***
.*.  *..  ..*
.*.  ***  ***

Input

The input contains only one test case, consisting of 6 lines. The first line contains n, the number of characters to recognize (1<=n<=10). Each of the next 5 lines contains 4n characters. Each character contains exactly 5 rows and 3 columns of characters followed by an empty column (filled with '.').

Output

The output should contain exactly one line, the recognized digits in one line.

Sample Input

3
.*..***.***.
.*....*...*.
.*..***.***.
.*..*.....*.
.*..***.***.

Output for the Sample Input

123

The Ninth Hunan Collegiate Programming Contest (2013) Problemsetter: Rujia Liu Special Thanks: Feng Chen, Md. Mahbubul Hasan

 方法很多,深入理解dfs即可方便解决此题 ,我觉得这个方法比较好,有一定的价值。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;

struct Point{
    int  X ;
    int  Y ;
    Point(){} ;
    Point(int x ,int y):X(x),Y(y){} ;
};

char str[48][48] ;
int N  ;
int d[4][2]={{1,0},{-1,0},{0,-1},{0,1}} ;
bool visited[48][48] ;
vector<Point>my_hash[18] ;


int cango(int x ,int y){
    return 1<=x&&x<=5&&1<=y&&y<=4*N&&str[x][y]=='*';
}

void dfs(int x ,int y ,int color){
     my_hash[color].push_back(Point(x,y)) ;
     visited[x][y]=1 ;
     for(int i=0;i<4;i++){
          int xx=x+d[i][0] ;
          int yy=y+d[i][1]  ;
          if(cango(xx,yy)&&!visited[xx][yy]){
               dfs(xx,yy,color) ;
          }
     }
}

int main(){
    int color=0 ;
    scanf("%d",&N) ;
    for(int i=1;i<=5;i++)
         scanf("%s",str[i]+1) ;
    for(int i=1;i<=N;i++)
        my_hash[i].clear() ;
    for(int i=1;i<=5;i++)
       for(int j=1;j<=4*N;j++){
           if(str[i][j]=='*'&&!visited[i][j]){
               color++ ;
               dfs(i,j,color) ;
           }
    }

    for(int i=1;i<=N;i++){
        Point first = my_hash[i][0] ;
        Point second = my_hash[i][my_hash[i].size()-1] ;
        if(first.Y==second.Y&&first.X+4==second.X)
            putchar('1') ;
        else if(first.Y<second.Y)
            putchar('2') ;
        else if(first.X+2==second.X)
            putchar('3') ;
    }
    puts("") ;
    return 0 ;
}

 

转载于:https://www.cnblogs.com/liyangtianmen/p/3367198.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值