UVA Solution 487 - Boggle Blitz - Solution in C++ - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Monday, May 22, 2017

UVA Solution 487 - Boggle Blitz - Solution in C++

UVA Solution 487 - Boggle Blitz - Volume 4


UVA Online Judge Solution 487 - Boggle Blitz | Volume 4
UVA Problem Link - 487 - Boggle Blitz code

Problem Name: 487 - Boggle Blitz solution
Problem Number : UVA - 487 - Boggle Blitz code
Online Judge : UVA Online Judge Solution
Volume: 4
Solution Language : C plus plus

UVA Online Judge Solution, UVA OJ Solution list, UVA Problems Solution, UVA solver, UVA all problem solution list

UVA Solution 487 - Boggle Blitz Code in CPP:

#include <stdio.h>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
char board[21][21], used[21][21] = {}, tmp[401];
set<string> ans;
int n;
void dfs(int x, int y, int idx) {
    if(idx >= 3) {
        tmp[idx] = '\0';
        ans.insert(tmp);
    }
    if(x < 0 || y < 0 || x >= n || y >= n)
        return;
    if(idx > 0 && board[x][y] <= tmp[idx-1])
        return;
    if(used[x][y])
        return;
    tmp[idx] = board[x][y];
    used[x][y] = 1;
    dfs(x+1, y, idx+1);
    dfs(x-1, y, idx+1);
    dfs(x, y+1, idx+1);
    dfs(x, y-1, idx+1);
    dfs(x-1, y-1, idx+1);
    dfs(x-1, y+1, idx+1);
    dfs(x+1, y+1, idx+1);
    dfs(x+1, y-1, idx+1);
    used[x][y] = 0;
}
bool cmp(string a, string b) {
    return a.length() < b.length();
}
int main() {
    int t, i, j;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        for(i = 0; i < n; i++) {
            scanf("%s", board[i]);
        }
        ans.clear();
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                dfs(i, j, 0);
            }
        }
        string tot[50000];
        int totIdx = 0;
        for(set<string>::iterator it = ans.begin();
            it != ans.end(); it++) {
            tot[totIdx++] = *it;
        }
        stable_sort(tot, tot+totIdx, cmp);
        for(i = 0; i < totIdx; i++)
            cout << tot[i] << endl;
        if(t)
            puts("");
    }
    return 0;
}


Tags: UVA Online Judge Solution, UVA OJ Solution list, UVA Problems Solution, UVA solver, UVA all problem solution list, UVA 487 code in C, UVA 487 - Boggle Blitz code in C++, UVA 487 - Boggle Blitzsolution in C, UVA 487 solution

No comments:

Post a Comment