社区讨论

感觉思路没啥问题 就是不对

P1101单词方阵参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lo19hoj5
此快照首次捕获于
2023/10/22 17:23
2 年前
此快照最后确认于
2023/11/02 17:25
2 年前
查看原帖
CPP
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

constexpr int N = 110;
char map[N][N], ans[N][N];
int n, group; 
bool st[N][N];

#define fore(i, a, n) for (int i = a; i <= n; i ++)

struct awa{
	int x, y;
}le[N];

const int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};  
const int dy[] = {1, 0, -1, 1, -1, 0, 1, -1};
const string str = "0yizhong"; // 记得下表要 -1

void print(char map[][110]){
	for (int i = 1; i <= n; i ++){
		for (int j = 1; j <= n; j ++)
			cout << map[i][j];
		cout << endl;
	}
}

void dfs(int x, int y, int num){ // 在(x, y)搜索 在搜num字母
	if (num > 7) return;
	
	for (int i = 0; i < 8; i ++){
		int xx = x + dx[i], yy = y + dy[i];
		if (xx < 1 || xx > n || yy < 1 || yy > n || st[xx][yy]) continue;
		
		// 这里我想的是 我只匹配一次 不会再搜第二次同一个字母
		if (map[xx][yy] == str[num]){
			ans[xx][yy] = str[num];
			st[xx][yy] = true;
			dfs(xx, yy, num + 1);
			st[xx][yy] = false;
		}
		
	}
}

int main(){
	memset(ans, '*', sizeof ans);
	cin >> n;
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= n; j ++){
			cin >> map[i][j];
			if (map[i][j] == 'y'){ // 找y开始搜
				group ++;
				le[group].x = i;
				le[group].y = j;
			}
		}			
	
	for (int i = 1; i <= group; i ++)
		dfs(le[i].x, le[i].y, 1);
		
	print(ans);
}
样例2运行结果:
CPP
*yizhong
gy**h***
n*i*****
o**z****
hh**h***
zz***o**
i****gng
yyyy*ggg

回复

1 条回复,欢迎继续交流。

正在加载回复...