Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. bool isValidCell(int x, int y)
  2. {
  3. if (x < 0 || y < 0 || x >= ROW || y >= COL)
  4. return false;
  5.  
  6. return true;
  7. }
  8.  
  9. void countPaths(int mat[ROW][COL], int x, int y, int visited[ROW][COL], int& count)
  10. {
  11. if (x == ROW - 1 && y == COL - 1)
  12. {
  13. count++;
  14. return;
  15. }
  16.  
  17.  
  18. visited[x][y] = 1;
  19. if (isValidCell(x, y) && mat[x][y])
  20. {
  21.  
  22. if (x + 1 < ROW && !visited[x + 1][y])
  23. countPaths(mat, x + 1, y, visited, count);
  24.  
  25.  
  26. if (x - 1 >= 0 && !visited[x - 1][y])
  27. countPaths(mat, x - 1, y, visited, count);
  28.  
  29.  
  30. if (y + 1 < COL && !visited[x][y + 1])
  31. countPaths(mat, x, y + 1, visited, count);
  32.  
  33.  
  34. if (y - 1 >= 0 && !visited[x][y - 1])
  35. countPaths(mat, x, y - 1, visited, count);
  36. }
  37.  
  38.  
  39. visited[x][y] = 0;
  40. }
  41.  
  42. int visited[ROW][COL];
  43. memset(visited, 0, sizeof visited);
  44. countPaths(mat, 0, 0, visited, count);
  45.  
  46. cout << "Number of unique paths are " << count<<endl;
  47.  
  48. #include <bits/stdc++.h>
  49. #include <iostream>
  50. #define ROW 9
  51. #define COL 10
  52.  
  53. using namespace std;
  54.  
  55.  
  56.  
  57.  
  58. struct Point
  59. {
  60. int x;
  61. int y;
  62. };
  63.  
  64.  
  65. struct queueNode
  66. {
  67. Point pt;
  68. int dist;
  69. };
  70.  
  71.  
  72. bool isValidCell(int x, int y)
  73. {
  74. if (x < 0 || y < 0 || x >= ROW || y >= COL)
  75. return false;
  76.  
  77. return true;
  78. }
  79.  
  80. void countPaths(int mat[ROW][COL], int x, int y, int visited[ROW][COL], int& count)
  81. {
  82. if (x == ROW - 1 && y == COL - 1)
  83. {
  84. count++;
  85. return;
  86. }
  87.  
  88.  
  89. visited[x][y] = 1;
  90. if (isValidCell(x, y) && mat[x][y])
  91. {
  92.  
  93. if (x + 1 < ROW && !visited[x + 1][y])
  94. countPaths(mat, x + 1, y, visited, count);
  95.  
  96.  
  97. if (x - 1 >= 0 && !visited[x - 1][y])
  98. countPaths(mat, x - 1, y, visited, count);
  99.  
  100.  
  101. if (y + 1 < COL && !visited[x][y + 1])
  102. countPaths(mat, x, y + 1, visited, count);
  103.  
  104.  
  105. if (y - 1 >= 0 && !visited[x][y - 1])
  106. countPaths(mat, x, y - 1, visited, count);
  107. }
  108.  
  109.  
  110. visited[x][y] = 0;
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117. int rowNum[] = {-1, 0, 0, 1};
  118. int colNum[] = {0, -1, 1, 0};
  119.  
  120.  
  121. int BFS(int mat[][COL], Point src, Point dest)
  122. {
  123.  
  124. if (!mat[src.x][src.y] || !mat[dest.x][dest.y])
  125. return -1;
  126.  
  127. bool visited[ROW][COL];
  128. memset(visited, false, sizeof visited);
  129.  
  130.  
  131. visited[src.x][src.y] = true;
  132.  
  133.  
  134. queue<queueNode> q;
  135.  
  136.  
  137. queueNode s = {src, 0};
  138. q.push(s);
  139.  
  140.  
  141. while (!q.empty())
  142. {
  143. queueNode curr = q.front();
  144. Point pt = curr.pt;
  145.  
  146.  
  147. if (pt.x == dest.x && pt.y == dest.y)
  148. return curr.dist;
  149.  
  150.  
  151. q.pop();
  152.  
  153. for (int i = 0; i < 4; i++)
  154. {
  155. int row = pt.x + rowNum[i];
  156. int col = pt.y + colNum[i];
  157.  
  158.  
  159. if (isValidCell(row, col) && mat[row][col] &&
  160. !visited[row][col])
  161. {
  162.  
  163. visited[row][col] = true;
  164. queueNode Adjcell = { {row, col},
  165. curr.dist + 1
  166. };
  167. q.push(Adjcell);
  168. }
  169. }
  170. }
  171.  
  172.  
  173. return -1;
  174. }
  175.  
  176. int main()
  177. {
  178. int Exits;
  179. int Sr, Sc, r, c;
  180.  
  181. int mat[ROW][COL] =
  182. {
  183. { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
  184. { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
  185. { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
  186. { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
  187. { 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
  188. { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 },
  189. { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  190. { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
  191. { 1, 1, 0, 0, 0, 0, 1, 0, 0, 1 }
  192. };
  193.  
  194.  
  195. cout<<" Set Starting Position [row] [col]: "<<endl;
  196. cin >> Sr;
  197. cin >> Sc;
  198. Point source = {Sr, Sc};
  199.  
  200. cout<<"Number of Exits: "<<endl;
  201. cin >> Exits;;
  202. int Steps[Exits];
  203. int MemR[Exits];
  204. int MemC[Exits];
  205. int Best_Path;
  206. int Best_Point;
  207.  
  208. for (int b = 0; b < Exits; b++)
  209. {
  210. Steps[b]=0;
  211. }
  212.  
  213. for (int i=0; i<Exits; i++)
  214. {
  215. cout<<"Type in coordinates for Exit nr."<<i+1<<endl;
  216. cin >> r;
  217. cin >> c;
  218. MemR[i]=r;
  219. MemC[i]=c;
  220.  
  221. mat[r][c]=2; //Dzięki temu Wyjścia są traktowane jako droga.
  222.  
  223. }
  224.  
  225.  
  226. cout<<"==========LABIRYNTH: =========="<<endl<<endl;
  227. cout << " ";
  228. for (int z = 0; z < COL; z++)
  229. {
  230. cout<<z<<" ";
  231. }
  232. cout<<endl;
  233.  
  234. for (int t = 0; t < ROW; t++)
  235. {
  236. cout<<t<<" ";
  237.  
  238. for (int u = 0; u < COL; u++)
  239. {
  240. if (t==Sr && u==Sc)
  241. cout << "S " << ' ';
  242.  
  243.  
  244. else if (mat[t][u]==2)
  245. {
  246. cout << "W " << ' ';
  247. }
  248.  
  249. else
  250. {
  251. if (mat[t][u]==0)
  252. cout << "X " << ' ';
  253. if (mat[t][u]==1)
  254. cout << "O " << ' ';
  255. }
  256.  
  257.  
  258.  
  259.  
  260. }
  261. cout << endl;
  262. }
  263.  
  264. cout<<endl<<"==========PATHS: =========="<<endl<<endl;
  265.  
  266. for (int j=0; j<Exits; j++)
  267. {
  268.  
  269. Point dest = {MemR[j], MemC[j]};
  270. int dist = BFS(mat, source, dest);
  271.  
  272. if (dist != INT_MAX && dist != -1)
  273. {
  274. Steps[j]=dist;
  275. cout << "Shortest path to point ["<<MemR[j]<<"] " <<"["<<MemC[j]<<"] equals "<<dist<<" steps"<<endl;
  276. int count = 0;
  277.  
  278.  
  279. int visited[ROW][COL];
  280. memset(visited, 0, sizeof visited);
  281. countPaths(mat, 0, 0, visited, count);
  282.  
  283. cout << "Number of unique paths are " << count<<endl;
  284. }
  285.  
  286.  
  287. else if (dist= -1)
  288. cout << "No path or exit is equal to 0 for point: ["<<MemR[j]<<"] " <<"["<<MemC[j]<<"]"<<endl;
  289. else
  290. cout << "No path for point: ["<<MemR[j]<<"] " <<"["<<MemC[j]<<"]"<<endl;
  291.  
  292.  
  293.  
  294.  
  295. }
  296.  
  297. cout<<endl<<"==========Best route: =========="<<endl<<endl;
  298. Best_Path = Steps[0];
  299. for (int n = 0; n < Exits; n++)
  300. {
  301. if (Best_Path > Steps[n] && Steps[n]!=0)
  302.  
  303. Best_Path = Steps[n];
  304. }
  305.  
  306.  
  307.  
  308. for (int z=0; z<Exits; z++)
  309. {
  310. if (Steps[z]==Best_Path && Best_Path!=0)
  311. {
  312. cout <<"Best route exists for point: ["<<MemR[z]<<"] " <<"["<<MemC[z]<<"] and equals "<<Best_Path<<" steps"<<endl;
  313. }
  314.  
  315. }
  316.  
  317.  
  318.  
  319.  
  320. return 0;
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement