this is my code when i run it and enter the input the run and after a second they throw a run time error i put some outputs to show where the problem is occur and i notice that it is occur in the function Dfs after the v reach the number 168535 but i don't know why this problem occurred...can you help me?
#include<bits/stdc++.h>
using namespace std;
typedef long l;
typedef long long ll;
typedef unsigned long long ull;
#define sc(a) scanf("%d",&a)
const int MAX = 1e6+5;
const int inf = 1e7+77;
vector<int> adj[MAX];
bool vis[MAX];
vector<int> leaf;
// 168 535 -> after this number the DFS throw run time error
void dfs(int v){
cout<<"v = "<<v<<endl;
leaf.push_back(v);
cout<<"running"<<endl;
vis[v] = 1;
cout<<"running"<<endl;
for(int i = 0 ; i < adj[v].size() ; ++i){
cout<<"adj[v][i] = "<<adj[v][i]<<endl;
if(!vis[adj[v][i]])
dfs(adj[v][i]);
}
}
int main(){
int n , m , k;
sc(n);
sc(m);
sc(k);
char maze[n][m];
for(int i = 0 ; i < n ; ++i)
for(int j = 0 ; j < m ; ++j)
maze[i][j] = '.';
if(k > 0){
int graph[n][m];
int o = 1;
for(int i = 0 ; i < n ; ++i)
for(int j = 0 ; j < m ; ++j){
graph[i][j] = o;
++o;
}
for(int i = 0 ; i < n ; ++i){
for(int j = 0 ; j < m ; ++j){
if(maze[i][j] == '.'){
if(i + 1 < n){
if(maze[i+1][j] == '.')
adj[graph[i][j]].push_back(graph[i+1][j]);
}
if(i - 1 >= 0){
if(maze[i-1][j] == '.')
adj[graph[i][j]].push_back(graph[i-1][j]);
}
if(j + 1 < m){
if(maze[i][j+1] == '.')
adj[graph[i][j]].push_back(graph[i][j+1]);
}
if(j - 1 >= 0){
if(maze[i][j-1] == '.')
adj[graph[i][j]].push_back(graph[i][j-1]);
}
}
}
}
int st;
bool b = 0;
for(int i = 0 ; i < n ; ++i){
for(int j = 0 ; j < m ; ++j){
if(maze[i][j] == '.'){
st = graph[i][j];
b = 1;
}
if(b)
break;
}
if(b)
break;
}
cout<<"st = "<<st<<endl;
dfs(st);
int s = leaf.size() - k;
sort(leaf.begin() + s , leaf.end());
for(int i = 0 ; i < n ; ++i){
for(int j = 0 ; j < m ; ++j){
if(graph[i][j] == leaf[s]){
maze[i][j] = 'X';
++s;
}
}
}
for(int i = 0 ; i < n ; ++i){
for(int j = 0 ; j < m ; ++j)
cout<<maze[i][j];
cout<<endl;
}
}
else
{
for(int i = 0 ; i < n ; ++i){
for(int j = 0 ; j < m ; ++j)
cout<<maze[i][j];
cout<<endl;
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire