There are many algorithms to generate maze.…
- User
There are many algorithms to generate maze. (//en.wikipedia.org/wiki/Maze_generation_algorithm). After generating the maze we’ve to validate whether it’s a valid maze or not. A valid maze has exactly one entry point and exactly one exit point (exactly 2 openings in the edges) and there must be at least one path from the entry point to exit point.
Given a maze, just find whether the maze is "valid" or "invalid".
Input The first line consists of an integer t, the number of test cases. Then for each test case, the first line consists of two integers m and n, the number of rows and columns in the maze. Then contains the description of the matrix M of order mxn. M[i][j]=# represents a wall and M[i][j]='.' represents a space.
Output For each test case find whether the maze is "valid" or "invalid".
Constraints 1<=t<=10000
1<=m<=20
1<=n<=20
Example Inputcopy Outputcopy 6 4 4
#... #.## #.## 5 5 #.### #..## ##..# #.#.# ###.# 1 1 . 5 1
. .
2 2 #. .# 3 4 #..# #.## #.##
valid valid invalid valid invalid invalid Bu sualda #include <bits/stdc++.h>
using namespace std;
#define ll long long #define ld long double #define endl '\n' #define pb push_back #define speedyboy ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) x.begin() , x.end() #define F first #define S second #define pll pair<ll , ll> #define pss pair<string , string> #define YES cout << "YES" << endl; #define NO cout << "NO" << endl;
const ll sz = 1e3 + 5 , INF = 1e18 , MOD = 998244353; bool used[sz][sz]; char c[sz][sz]; const ll dx[4] = {1 , -1 , 0 , 0}; const ll dy[4] = {0 , 0 , -1 , 1}; ll path; bool ok;
bool check(ll x , ll y , ll n , ll m){ return x >= 1 && x <= n && y >= 1 && y <= m; }
void dfs(ll x , ll y , ll n , ll m){ used[x][y] = true; path++; for(ll i = 0 ; i < 4 ; i++){ ll nx = x + dx[i] , ny = y + dy[i]; if(path >= 2 && c[nx][ny] != '.' && c[nx][ny] != '#') ok = true; } for(ll i = 0 ; i < 4 ; i++){ ll nx = x + dx[i] , ny = y + dy[i]; if(check(nx , ny , n , m) && !used[nx][ny] && c[nx][ny] == '.'){ dfs(nx , ny , n , m); } } }
ll i , j , k; void solve(){ ll n , m; cin >> n >> m; for(i = 1 ; i <= n ; i++){ for(j = 1 ; j <= m ; j++){ cin >> c[i][j]; } } for(i = 1 ; i <= n ; i++){ for(j = 1 ; j <= m; j++){ for(k = 0 ; k < 4 ; k++){ ll nx = i + dx[k] , ny = j + dy[k]; if(c[nx][ny] != '#' && c[nx][ny] != '.'){ path = 0; dfs(i , j , n , m); if(ok){ cout << "valid" << endl; return; } } } } } cout << "invalid" << endl; }
signed main(){ speedyboy; ll t = 1; cin >> t; while(t--){ solve(); } } mənim kodum niyə işləmir?
- Assistant