CSU 1719(智障选手的DFS)

1719: Boggle

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 253   Solved: 28
[ Submit][ Status][ Web Board]

Description

Input

Output

Sample Input

3
april
purple
quilt
5
rprit
ahqln
ietep
zrysg
ogwey
3
pel
aup
bcr
0

Sample Output

april
quilt
-
purple
-

HINT

Source




题意:有n个单词,然后给你一个m*m的字符矩阵,让你找出一条路径使得走过的路径是一个单词,方向是八个方向都可以走。问有哪些单词可以匹配?这里如果字符矩阵里出现了q,那么把他看作是qu


题解:智障选手写了好久的题,本来很简单的暴力DFS,可是忘记加标记访问。


这里给出2份代码,第一份直接爆搜,第二份是将单词中所有的qu转化为q,这样搜起来更简单一点


第一份:直接爆搜

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<set>  
#include<queue>  
#include<string>  
#include<bitset>  
#include<utility>  
#include<functional>  
#include<iomanip>  
#include<sstream>  
#include<ctime>  
using namespace std;

#define N int(3e3)  
#define inf int(0x3f3f3f3f)  
#define mod int(1e9+7)  
typedef long long LL;

int vx[] = { 0,1,0,-1,1,-1,1,-1 };
int vy[] = { 1,0,-1,0,-1,1,1,-1 };

char s[N][N];
char g[N][N];
int vis[N][N];
int t, n;
bool judge(int x, int y)
{
	if (x<0 || x >= n || y<0 || y >= n)
		return 0;
	if (vis[x][y])return 0;
	return 1;
}
int ok = 0;
void dfs(int x, int y, int dep, int num, int length)
{
	if (dep == length)
	{
		ok = 1;
		return;
	}
	if (ok || dep>length)
	{
		return;
	}
	for (int i = 0; i<8; i++)
	{
		int xx = x + vx[i];
		int yy = y + vy[i];
		if (judge(xx, yy))
		{
			if (g[xx][yy] == s[num][dep])
			{
				vis[xx][yy] = 1;
				if (s[num][dep] == 'q'&&s[num][dep + 1] == 'u')
				{
					dfs(xx, yy, dep + 2, num, length);
				}
				else if (s[num][dep] != 'q')
				{
					dfs(xx, yy, dep + 1, num, length);
				}
				vis[xx][yy] = 0;
			}
		}
	}
}
vector<string>ans;
void solve()
{
	ans.clear();
	memset(vis, 0, sizeof(vis));
	for (int k = 0; k<t; k++)
	{
		ok = 0;
		for (int i = 0; i<n; i++)
		{
			for (int j = 0; j<n; j++)
			{
				if (s[k][0] == g[i][j] && s[k][0] == 'q'&&s[k][1] == 'u')
				{
					vis[i][j]=1;
					dfs(i, j, 2, k, strlen(s[k]));
					vis[i][j]=0;
				}
				else  if (s[k][0] == g[i][j] && s[k][0] != 'q')
				{
					vis[i][j]=1;
					dfs(i, j, 1, k, strlen(s[k]));
					vis[i][j]=0;
				}
				if (ok)break;
			}
			if (ok)break;
		}
		if (ok)
		{
			ans.push_back(string(s[k]));
		}
	}
	sort(ans.begin(), ans.end());
	for (int i = 0; i<ans.size(); i++)
	{
		printf("%s\n", ans[i].c_str());
	}
}

int main()
{
#ifdef CDZSC  
	freopen("i.txt", "r", stdin);
	//freopen("o.txt","w",stdout);  
	int _time_jc = clock();
#endif  
	while (~scanf("%d", &t))
	{
		for (int i = 0; i < t; i++)
		{
			scanf("%s", s[i]);
		}
		while (~scanf("%d", &n))
		{
			if (n == 0)break;
			for (int i = 0; i < n; i++)
				scanf("%s", g[i]);
			solve();
			puts("-");
		}
	}
	return 0;
}


第二份:将单词中所有的qu转化为q,这样搜起来更简单一点



#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<set>  
#include<queue>  
#include<string>  
#include<bitset>  
#include<utility>  
#include<functional>  
#include<iomanip>  
#include<sstream>  
#include<ctime>  
using namespace std;

#define N int(1e3)  
#define inf int(0x3f3f3f3f)  
#define mod int(1e9+7)  
typedef long long LL;

int vx[] = { 0,1,0,-1,1,-1,1,-1 };
int vy[] = { 1,0,-1,0,-1,1,1,-1 };

char s[N][N];
char g[N][N];
int vis[N][N];
int t, n;
map<string, string>st;
bool judge(int x, int y)
{
	if (x<0 || x >= n || y<0 || y >= n)
		return 0;
	if (vis[x][y])return 0;
	return 1;
}
int ok = 0;
void dfs(int x, int y, int dep, int num, int length)
{
	if (dep == length)
	{
		ok = 1;
		return;
	}
	if (ok || dep>length)
	{
		return;
	}
	for (int i = 0; i<8; i++)
	{
		int xx = x + vx[i];
		int yy = y + vy[i];
		if (judge(xx, yy))
		{
			if (g[xx][yy] == s[num][dep])
			{
				vis[xx][yy] = 1;
				dfs(xx, yy, dep + 1, num, length);
				vis[xx][yy] = 0;
			}
		}
	}
}
vector<string>ans;
void solve()
{
	ans.clear();
	memset(vis, 0, sizeof(vis));
	for (int k = 0; k<t; k++)
	{
		ok = 0;
		for (int i = 0; i<n; i++)
		{
			for (int j = 0; j<n; j++)
			{
				if (s[k][0] == g[i][j])
				{
					vis[i][j] = 1;
					dfs(i, j, 1, k, strlen(s[k]));
					vis[i][j] = 0;
				}
				if (ok)break;
			}
			if (ok)break;
		}
		if (ok)
		{
			ans.push_back(st[string(s[k])]);
		}
	}
	sort(ans.begin(), ans.end());
	for (int i = 0; i<ans.size(); i++)
	{
		printf("%s\n", ans[i].c_str());
	}
}

int main()
{
#ifdef CDZSC  
	freopen("i.txt", "r", stdin);
	//freopen("o.txt","w",stdout);  
	int _time_jc = clock();
#endif  
	scanf("%d", &t);
	for (int i = 0; i<t; i++)
	{
		string tmp;
		scanf("%s", s[i]);
		for (int j = 0; s[i][j]; j++)
		{
			if (s[i][j] == 'q'&&s[i][j + 1] == 'u')
			{
				j++;
				tmp += "q";
			}
			else if (s[i][j] == 'q')
			{
				tmp += "#";
			}
			else
			{
				tmp += s[i][j];
			}
		}
		st[tmp] = string(s[i]);
		for (int j = 0; j<tmp.size(); j++)
		{
			s[i][j] = tmp[j];
		}
		s[i][tmp.size()] = '\0';
	}
	while (~scanf("%d", &n) && n)
	{
		for (int i = 0; i<n; i++)
			scanf("%s", g[i]);
		solve();
		puts("-");
	}
	return 0;
}




你可能感兴趣的:(CSU 1719(智障选手的DFS))