zoukankan      html  css  js  c++  java
  • 单词方阵【DFS】

    image

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 constexpr size_t maxn = 105;
      4 int dx[9]={1,0,1,-1,0,-1,1,-1};//八个方位 
      5 int dy[9]={0,1,1,0,-1,-1,-1,1};
      6 char mp[maxn][maxn];
      7 bool vis[maxn][maxn];
      8 int pos[9][2], n;
      9 string standard = "yizhong";
     10 bool judge(int x, int y){
     11 	return x >= 0 && x < n && y >= 0 && y < n;
     12 }
     13 void dfs(int x, int y, int p, int k, int cnt){
     14 	//cout << x << " " << y << endl;
     15 	pos[cnt][0]= x;
     16 	pos[cnt][1] = y;
     17 	if(standard[p] == 'g')
     18 	{
     19 		//cout << 1 << endl; 
     20 		for(int i = 0; i < 7; ++ i){
     21 			vis[pos[i][0]][pos[i][1]] = true;
     22 		}
     23 		return;
     24 	}
     25 	int nx = x + dx[k];
     26 	int ny = y + dy[k];
     27 
     28 	if(judge(x, y) && mp[nx][ny] == standard[p + 1]){
     29 		dfs(nx, ny, p + 1, k, cnt + 1);
     30 	}
     31 
     32 }
     33 
     34 
     35 int main(){
     36 	//freopen("input.txt","r",stdin);
     37 
     38 
     39 	cin >> n;
     40 	for(int i = 0; i < n; ++ i){
     41 		for(int j = 0; j < n; ++ j){
     42 			cin >> mp[i][j];
     43 		}
     44 	}
     45 	for(int i = 0; i < n; ++ i){
     46 		for(int j = 0; j < n; ++ j){
     47 
     48 			if(mp[i][j] == 'y'){
     49 				for(int k = 0; k < 8; ++ k)
     50 				{
     51 					int x = i + dx[k];
     52 					int y = j + dy[k];
     53 					if(mp[x][y] == 'i'){
     54 						if(judge(x, y)){
     55 							pos[0][0] = i;
     56 							pos[0][1] = j;
     57 							//cout << endl;
     58 							//cout << i << " " << j << endl;
     59 
     60 							dfs(x, y, 1, k, 1);
     61 						}
     62 					}
     63 				}
     64 			}
     65 		}
     66 	}
     67 
     68 	for(int i = 0; i < n; ++ i){
     69 		for(int j = 0; j < n; ++ j){
     70 			if(vis[i][j])
     71 			cout << mp[i][j];
     72 			else
     73 			cout << '*';
     74 		}
     75 		cout << endl;
     76 	}
     77 
     78 }
    追求吾之所爱
  • 相关阅读:
    nfs共享目录及sersync实时同步
    rsync备份
    MySQL基础操作
    源码包安装MySQL
    二进制安装MySQL
    Centos6防火墙-iptables版
    linux系统mongdb基础(1)
    linux系统ElK基础filebeat收集日志(4)
    linux系统ElK基础(3)
    linux系统ElK基础(2)
  • 原文地址:https://www.cnblogs.com/rstz/p/14391063.html
Copyright © 2011-2022 走看看