Moderator: Board moderators

/*
Problem No: 208
Problem : Firetruck
Algorithm: DFS
Author : Ishtiaq Ahmed
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maximum(aa, bb) ((aa) > (bb) ? (aa) : (bb))
int destination;
int counting = 0;
bool array[1000][1000] = {false};
bool visited[1000] = {false};
int result[1000], indexx;
void DFS(int current, int max){
int i;
if(current == destination){
result[++indexx] = current;
for(i = 0; i <= indexx; i++){
if(indexx == i)
printf("%d\n", result[i]);
else
printf("%d ", result[i]);
}
counting++;
return;
}
result[++indexx] = current;
visited[current] = true;
for(i = 1; i <= max; i++){
if(array[current][i] == true && visited[i] == false){
DFS(i, max);
visited[i] = false;
indexx--;
}
}
}
int main(){
//freopen("c:\\input.txt", "r", stdin);
//freopen("c:\\out.txt", "w", stdout);
int casno = 0, i, j, a, b;
int max = -99999;
while(scanf("%d", &destination) != EOF){
printf("CASE %d:\n", ++casno);
counting = 0;
indexx = -1;
for(i = 1; i <= max; i++)
for(j = 1; j <= max; j++)
array[i][j] = false;
for(i = 1; i <= max; i++)
visited[i] = false;
max = -9999999;
while(scanf("%d %d", &a, &b)){
if(!a && !b)
break;
array[a][b] = true;
array[b][a] = true;
max = maximum(max, a);
max = maximum(max, b);
}
DFS(1, max);
printf("There are %d routes from the firestation to streetcorner %d.\n", counting, destination);
}
return 0;
}
thales wrote:Actually, I can not understand the idea of prunning backtracking with Floyd.
In backtracking, if I arrived to a node A from which the final node T is unreachable, still Floyd will give value
less than infinite, since from A I can reach T with some node I already used.
So, Floyd will not reject the specific node since with the matrix I will not be able to know the nodes I need to travel
from A to T.
Thanks in advance for any help
Code Removed
1 4 2 3 6
1 5 3 6
There are 2 routes from the firestation to streetcorner 6.14
1 8
2 11
3 4
3 6
4 14
5 6
5 8
6 11
6 12
8 14
9 14
10 14
11 14
0 0CASE 1:
1 8 5 6 3 4 14
1 8 5 6 11 14
1 8 14
There are 3 routes from the firestation to streetcorner 14.Users browsing this forum: No registered users and 1 guest