[蓝桥杯2018初赛]字母阵列

原题链接
思路:总共八个方向,不需要搜索。直接每次搜的方向都是一样的,遇到边界直接退出即可。其中vis数组是打印自己搜的情况是否正确。

#include<bits/stdc++.h>

using namespace std;
int vis[105][105];
int dir[8][2]={0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0,-1,1};
int main()
{
	string e[105];
	for(int i=0;i<100;i++)
	cin>>e[i];
//	cout<<"-------------------"<<'\n';
//	for(int i=0;i<100;i++){
//		cout<<s[i]<<"\n";
//	}
	int cnt=0;
	for(int i=0;i<100;i++){
		for(int j=0;j<100;j++){
			if(e[i][j]=='L'){
				for(int k=0;k<8;k++){
					int x=i,y=j;
					string res="L";
					memset(vis,0,sizeof(vis));
					for(int m=0;m<6;m++){
						x=x+dir[k][0];
						y=y+dir[k][1];
						if(x>=0&&x<100&&y>=0&&y<100&&!vis[x][y]){
							vis[x][y]=1;
							res+=e[x][y];
						}else break;
					}
					if(res=="LANQIAO"){
						cnt++;
//						for(int a=0;a<100;a++){
//							for(int b=0;b<100;b++){
//								if(vis[a][b])cout<<e[a][b];
//								
//							}
//							cout<<"\n";
//						}
//						cout<<"----------------"<<'\n';
					}
					
				}
			}
		}
	}
	cout<<cnt<<'\n';
	return 0;
}
//LANQIAO---41

猜你喜欢

转载自blog.csdn.net/qq_43566782/article/details/108814921