Playing Boggle

#include<bits/stdc++.h> using namespace std; char board[4][4]; bool visited[4][4]; int dr[] = {1 ,1 ,1, 0, -1, -1, -1, 0}; int dc[] = {-1, 0, 1, 1, 1, 0, -1, -1}; bool dfs(int row, int col, string s, int index); int score(int x); int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; for(int z = 1; z <= t; z++){ //memset(visited, false, sizeof visited); for (int i = 0; i < 4; i++) cin>>board[i]; int m; cin>>m; vector<string> dict; dict.reserve(m); string temp; while(m--){ cin>>temp; dict.push_back(temp); } int ans = 0; for (int i = 0, len = dict.size(); i < len; i++){ for(int j = 0; j < 4; j++){ for(int k = 0; k < 4; k++){ if (board[j][k] == dict[i][0]){ memset(visited, false, sizeof visited); if(dfs(j,k,dict[i],0)){ ans += score(dict[i].length()); //cout<<dict[i]<<endl; j = 4; break; } } } } } printf("Score for Boggle game #%d: %d\n", z, ans); } } int score(int x){ if (x < 5) return 1; if (x < 7) return x - 3; if (x == 7) return 5; else return 11; } bool dfs(int row, int col, string s, int index){ visited[row][col] = true; if (index == s.length() - 1) return true; bool found = false; for(int i = 0; i < 8; i++){ if (row + dr[i] >= 0 && col + dc[i] >= 0 && row + dr[i] < 4 && col + dc[i] < 4){ if (visited[row + dr[i]][col + dc[i]] == false && s[index + 1] == board[row + dr[i]][col + dc[i]]){ found = dfs(row + dr[i], col + dc[i], s, index + 1); if (found == true) return true; } } } visited[row][col] = false; return false; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.