洛谷P1101、P1019

洛谷P1101

happy 2020年A的第一道题~(~ ̄▽ ̄)~

题目大意:在一个二维字符数组中找到所有的连续’yizhong’的字符(各个方向),然后将其他的地方都用’*'替换掉。
解题思路:最开始的思路是在每层中都遍历八个方向,跑了一次之后发现结果有问题,后来改成了多判断一次的,看第二个字符是否为i,这样确定了方向,直接在每一次判断的时候把方向index往下一层传即可(即多传一个参数。)

//
//  main.cpp
//  p1101
//
//  Created by 陈冉飞 on 2020/1/31.
//  Copyright © 2020 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
#define maxn 105
#include <cstring>
char a[maxn][maxn],ans[maxn][maxn],st[7] = {'y','i','z','h','o','n','g'};
int n,dir[8][2] = {{1,1},{1,0},{1,-1},{-1,1},{-1,0},{0,-1},{0,1},{-1,-1}},cnt = 0;

void change(int x,int y,int index){
    for (int i = 0; i < 7; i++) ans[x-i*dir[index][0]][y-i*dir[index][1]] = st[6-i];
}

bool check(int x,int y,int l){
    if (x >=0 && x < n && y >= 0 && y < n && a[x][y] == st[l]) return true;
    return false;
}

void dfs(int x,int y,int l,int index){
    if (l == 6) {
        change(x,y,index);
        return;
    }
    if (check(x+dir[index][0], y+dir[index][1], l+1)) dfs(x+dir[index][0], y+dir[index][1], l+1, index);
}

void test(int x,int y){
    if (a[x][y] == 'y')
        for (int i = 0; i < 8; i++)
            if (a[x+dir[i][0]][y+dir[i][1]] == 'i')
                dfs(x+dir[i][0], y+dir[i][1],1,i);
}

int main(int argc, const char * argv[]) {
    scanf("%d",&n);
    for (int i = 0; i < n; i++) scanf("%s",a[i]);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            ans[i][j] = '*';
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            test(i, j);
    for (int i = 0; i < n; i++)
        printf("%s\n",ans[i]);
    return 0;
}

发布了95 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43345204/article/details/104125577
今日推荐