原题地址:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2258
#include <cstdio>
#include <cstring>
const int N = 6;
bool vis[N][N];
char s[N][N], word[18];
bool dfs(int x, int y, int k)
{
if(k>=strlen(word)) return true;
if(s[x][y]!=word[k] || vis[x][y]) return false;
vis[x][y]=1;
if(dfs(x-1, y-1, k+1)) return true;
if(dfs(x-1, y, k+1)) return true;
if(dfs(x-1, y+1, k+1)) return true;
if(dfs(x, y-1, k+1)) return true;
if(dfs(x, y+1, k+1)) return true;
if(dfs(x+1, y-1, k+1)) return true;
if(dfs(x+1, y, k+1)) return true;
if(dfs(x+1, y+1, k+1)) return true;
vis[x][y]=0;
return false;
}
bool solve(int k)
{
for(int i=1; i<5; i++) {
for(int j=1; j<5; j++) {
if(word[k]==s[i][j]) {
if(dfs(i, j, 0)) return true;
}
}
}
return false;
}
int point(int n)
{
if(n==3||n==4) return 1;
else if(n==5) return 2;
else if(n==6) return 3;
else if(n==7) return 5;
else return 11;
}
int main()
{
//freopen("in", "r", stdin);
int n, m, k;
scanf("%d", &n);
for(int i=1; i<=n; i++) {
printf("Score for Boggle game #%d: ", i);
memset(vis, 0, sizeof(vis));
memset(s, 0, sizeof(s));
k = 0;
for(int i=1; i<5; i++) scanf("%s", s[i]+1);
//for(int i=1; i<5; i++) printf("%s\n", s[i]+1);
scanf("%d", &m);
while(m--) {
scanf("%s", word);
memset(vis, 0, sizeof(vis));
if(solve(0)) k+=point(strlen(word));
}
printf("%d\n", k);
}
return 0;
}

2028

被折叠的 条评论
为什么被折叠?



