Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <string>
- using namespace std;
- char tab[2000][2000];
- void bfsCitiesSearch(int n, int m, int whichRow, int whichCol, int &count, int isStartingPoint)
- {
- if(whichRow >= n || whichCol >= m)
- return ;
- switch(tab[whichRow][whichCol])
- {
- case 'v':
- {
- if(whichRow + 1 == n && whichCol + 1 == m)
- return ;
- if(whichCol + 1 == m)
- bfsCitiesSearch(n, m, whichRow + 1, 0, count, 1);
- else
- bfsCitiesSearch(n, m, whichRow, whichCol + 1, count, 1);
- }
- break;
- case 'A':
- {
- if(whichRow + 1 == n && whichCol + 1 == m)
- return ;
- tab[whichRow][whichCol] = 'v';
- if(whichCol + 1 == m)
- bfsCitiesSearch(n, m, whichRow + 1, 0, count, 1);
- else
- bfsCitiesSearch(n, m, whichRow, whichCol + 1, count, 1);
- }
- break;
- case 'B':
- {
- tab[whichRow][whichCol] = 'v';
- if(isStartingPoint == 1)
- count++;
- if(whichCol - 1 >= 0)
- {
- char left = tab[whichRow][whichCol - 1];
- if(left == 'D' || left == 'E' || left == 'F')
- bfsCitiesSearch(n, m, whichRow, whichCol - 1, count, 0);
- }
- if(whichRow + 1 < n)
- {
- char down = tab[whichRow + 1][whichCol];
- if(down == 'C' || down == 'D' || down == 'F')
- bfsCitiesSearch(n, m, whichRow + 1, whichCol, count, 0);
- }
- if(whichCol + 1 == m && whichRow + 1 == n)
- return ;
- else
- {
- if(isStartingPoint == 1)
- {
- if(whichCol + 1 == m)
- bfsCitiesSearch(n, m, whichRow + 1, 0, count, 1);
- else
- bfsCitiesSearch(n, m, whichRow, whichCol + 1, count, 1);
- }
- }
- }
- break;
- case 'C':
- {
- tab[whichRow][whichCol] = 'v';
- if(isStartingPoint == 1)
- count++;
- if(whichCol - 1 >= 0)
- {
- char left = tab[whichRow][whichCol - 1];
- if(left == 'D' || left == 'E' || left == 'F')
- bfsCitiesSearch(n, m, whichRow, whichCol - 1, count, 0);
- }
- if(whichRow - 1 >= 0)
- {
- char up = tab[whichRow - 1][whichCol];
- if(up == 'B' || up == 'E' || up == 'F')
- bfsCitiesSearch(n, m, whichRow - 1, whichCol, count, 0);
- }
- if(whichCol + 1 == m && whichRow + 1 == n)
- return ;
- else
- {
- if(isStartingPoint == 1)
- {
- if(whichCol + 1 == m)
- bfsCitiesSearch(n, m, whichRow + 1, 0, count, 1);
- else
- bfsCitiesSearch(n, m, whichRow, whichCol + 1, count, 1);
- }
- }
- }
- break;
- case 'D':
- {
- tab[whichRow][whichCol] = 'v';
- if(isStartingPoint == 1)
- count++;
- if(whichCol + 1 < m)
- {
- char right = tab[whichRow][whichCol + 1];
- if(right == 'B' || right == 'C' || right == 'F')
- bfsCitiesSearch(n, m, whichRow, whichCol + 1, count, 0);
- }
- if(whichRow - 1 >= 0)
- {
- char up = tab[whichRow - 1][whichCol];
- if(up == 'B' || up == 'E' || up == 'F')
- bfsCitiesSearch(n, m, whichRow - 1, whichCol, count, 0);
- }
- if(whichCol + 1 == m && whichRow + 1 == n)
- return ;
- else
- {
- if(isStartingPoint == 1)
- {
- if(whichCol + 1 == m)
- bfsCitiesSearch(n, m, whichRow + 1, 0, count, 1);
- else
- bfsCitiesSearch(n, m, whichRow, whichCol + 1, count, 1);
- }
- }
- }
- break;
- case 'E':
- {
- tab[whichRow][whichCol] = 'v';
- if(isStartingPoint == 1)
- count++;
- if(whichCol + 1 < m)
- {
- char right = tab[whichRow][whichCol + 1];
- if(right == 'B' || right == 'C' || right == 'F')
- bfsCitiesSearch(n, m, whichRow, whichCol + 1, count, 0);
- }
- if(whichRow + 1 < n)
- {
- char down = tab[whichRow + 1][whichCol];
- if(down == 'C' || down == 'D' || down == 'F')
- bfsCitiesSearch(n, m, whichRow + 1, whichCol, count, 0);
- }
- if(whichCol + 1 == m && whichRow + 1 == n)
- return ;
- else
- {
- if(isStartingPoint == 1)
- {
- if(whichCol + 1 == m)
- bfsCitiesSearch(n, m, whichRow + 1, 0, count, 1);
- else
- bfsCitiesSearch(n, m, whichRow, whichCol + 1, count, 1);
- }
- }
- }
- break;
- case 'F':
- {
- tab[whichRow][whichCol] = 'v';
- if(isStartingPoint == 1)
- count++;
- if(whichCol + 1 < m)
- {
- char right = tab[whichRow][whichCol + 1];
- if(right == 'B' || right == 'C' || right == 'F')
- bfsCitiesSearch(n, m, whichRow, whichCol + 1, count, 0);
- }
- if(whichRow + 1 < n)
- {
- char down = tab[whichRow + 1][whichCol];
- if(down == 'C' || down == 'D' || down == 'F')
- bfsCitiesSearch(n, m, whichRow + 1, whichCol, count, 0);
- }
- if(whichCol - 1 >= 0)
- {
- char left = tab[whichRow][whichCol - 1];
- if(left == 'D' || left == 'E' || left == 'F')
- bfsCitiesSearch(n, m, whichRow, whichCol - 1, count, 0);
- }
- if(whichRow - 1 >= 0)
- {
- char up = tab[whichRow - 1][whichCol];
- if(up == 'B' || up == 'E' || up == 'F')
- bfsCitiesSearch(n, m, whichRow - 1, whichCol, count, 0);
- }
- if(whichCol + 1 == m && whichRow + 1 == n)
- return ;
- else
- {
- if(isStartingPoint == 1)
- {
- if(whichCol + 1 == m)
- bfsCitiesSearch(n, m, whichRow + 1, 0, count, 1);
- else
- bfsCitiesSearch(n, m, whichRow, whichCol + 1, count, 1);
- }
- }
- }
- break;
- }
- }
- int main()
- {
- int n,m; //rows and colums of a field
- int max = 2000;
- char text[max];
- char *line;
- line = fgets(text,max,stdin);
- sscanf(line,"%d %d",&n,&m);
- int i = 0; //to iterate over main loop
- int j = 0; //to iterate over tab
- int k = 0; //to iterate over line
- //assigning values to an array
- while(i < n)
- {
- line = fgets(text,max,stdin);
- while(k < m)
- {
- tab[i][k] = line[k];
- k++;
- }
- k = 0;
- i++;
- }
- int count = 0;
- bfsCitiesSearch(n, m, 0, 0, count, 1);
- printf("%d",count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement