Catalog
洛谷P1101单词方阵
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 100 + 10;
int dir[][2] = { {0,1},{1,1} ,{1,0},{1,-1},{0,-1},{-1,-1} ,{-1,0} ,{-1,1} };//八个方向
char fz[maxn][maxn];//存储字母的矩阵
int vis[maxn][maxn];//遍历矩阵表示是否被访问过
char stand[] = "yizhong";//标准单词
struct node {
int x, y;
}c[maxn];//用来记录路径
void dfs(int x,int y,int k,int cur) {
if (cur == 7)
for (int i = 0; i < 7; i++)
vis[c[i].x][c[i].y] = 1;//表示被访问过
else {
int dx = x + dir[k][0];
int dy = y + dir[k][1];
if (cur == 6 || fz[dx][dy] == stand[cur + 1])
{
c[cur].x = x; c[cur].y = y;//记录路径
dfs(dx, dy, k, cur + 1);//递归
}

}

}

int main() {
int n;
cin >> n;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
cin >> fz[i];
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
if(fz[i][j] == 'y')
for (int k = 0; k < 8; k++) {//定好方向
int dx = i + dir[k][0];
int dy = j + dir[k][1];
if (fz[dx][dy] == 'i')
dfs(i, j, k, 0);
}

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
if (!vis[i][j]) cout << "*";//没被标记过就输出*;
else cout << fz[i][j];
cout << endl;
}

}
Author: superzhaoyang
Link: http://yoursite.com/2019/11/21/洛谷P1101单词方阵/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付宝
��