1 Star 0 Fork 1

saigon/Algorithms

forked from charlieshu/Algorithms 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
P1101 单词方阵_可做模板.cpp 2.66 KB
一键复制 编辑 原始数据 按行查看 历史
charlie 提交于 2024-01-09 00:01 . move from github to gitee
#include <iostream>
#include <queue>
#include <string>
#include <string.h>
using namespace std;
string aim="yizhong";
bool dfs(int x,int y,int n,int index,char *a,bool *vis,int dir){
if(index >= aim.length())
return true;
if(x < 0 || x >= n || y < 0 || y >= n)
return false;
if(index == 0){
bool is=false;
is = dfs(x-1,y,n,index+1,a,vis,0)||is;
is = dfs(x-1,y-1,n,index+1,a,vis,1)||is;
is = dfs(x,y-1,n,index+1,a,vis,2)||is;
is = dfs(x+1,y-1,n,index+1,a,vis,3)||is;
is = dfs(x+1,y,n,index+1,a,vis,4)||is;
is = dfs(x+1,y+1,n,index+1,a,vis,5)||is;
is = dfs(x,y+1,n,index+1,a,vis,6)||is;
is = dfs(x-1,y+1,n,index+1,a,vis,7)||is;
if(is){
vis[y*n+x] = true;
return true;
}
}
else{
if(dir == 0){
if(a[y*n+x] == aim[index]){
if(dfs(x-1,y,n,index+1,a,vis,dir)){
vis[y*n+x] = true;
return true;
}
}
return false;
}
else if(dir == 1){
if(a[y*n+x] == aim[index]){
if(dfs(x-1,y-1,n,index+1,a,vis,dir)){
vis[y*n+x] = true;
return true;
}
}
return false;
}
else if(dir == 2){
if(a[y*n+x] == aim[index]){
if(dfs(x,y-1,n,index+1,a,vis,dir)){
vis[y*n+x] = true;
return true;
}
}
return false;
}
else if(dir == 3){
if(a[y*n+x] == aim[index]){
if(dfs(x+1,y-1,n,index+1,a,vis,dir)){
vis[y*n+x] = true;
return true;
}
}
return false;
}
else if(dir == 4){
if(a[y*n+x] == aim[index]){
if(dfs(x+1,y,n,index+1,a,vis,dir)){
vis[y*n+x] = true;
return true;
}
}
return false;
}
else if(dir == 5){
if(a[y*n+x] == aim[index]){
if(dfs(x+1,y+1,n,index+1,a,vis,dir)){
vis[y*n+x] = true;
return true;
}
}
return false;
}
else if(dir == 6){
if(a[y*n+x] == aim[index]){
if(dfs(x,y+1,n,index+1,a,vis,dir)){
vis[y*n+x] = true;
return true;
}
}
return false;
}
else if(dir == 7){
if(a[y*n+x] == aim[index]){
if(dfs(x-1,y+1,n,index+1,a,vis,dir)){
vis[y*n+x] = true;
return true;
}
}
return false;
}
}
}
int main(){
int n;
cin>>n;
char a[n][n];
bool vis[n][n];
memset(vis,false,sizeof(vis));
queue<int> q;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
if(a[i][j] == aim[0]){
q.push(i);
q.push(j);
}
}
}
while(!q.empty()){
int x,y;
y = q.front();
q.pop();
x = q.front();
q.pop();
dfs(x,y,n,0,&a[0][0],&vis[0][0],-1);
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(vis[i][j] == false)
cout<<"*";
else
cout<<a[i][j];
}
cout<<endl;
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/saigonshu/algorithm.git
git@gitee.com:saigonshu/algorithm.git
saigonshu
algorithm
Algorithms
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385