Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. --Shortest Path
  2. #include <iostream>
  3. #define F 99999
  4. #define V 4
  5. using namespace std;
  6. void printShortestPath(int dist[][V]){
  7.  
  8.     for (int i = 0; i < V; i++)
  9.     {
  10.         for (int j = 0; j < V; j++)
  11.         {
  12.             if (dist[i][j] == F)
  13.                 cout<<"F\t";
  14.             else
  15.                 cout<<dist[i][j]<<"\t";
  16.         }
  17.         cout<<"\n";
  18.     }
  19. }
  20. void Floyd (int graph[][V]){
  21.     int dist[V][V],i,j,k;
  22.     //Khoi tao bang ma tran dist giup luu vet
  23.     for(i=0;i<V;i++){
  24.         for(j=0;j<V;j++){
  25.             dist[i][j]= graph[i][j];
  26.         }
  27.     }
  28.     //Thuạt toan Floyd
  29.     // Kiem tra  dinh k neu do dai tu diem i toi j thong qua k ngan hon thi thay do dai tu i toi j hien tại thanh do dai moi tinh
  30.     for(k=0;k<V;k++){
  31.         for(i=0;i<V;i++){
  32.             for(j=0;j<V;j++){
  33.                 if(dist[i][k] != F && dist[k][j] != F && dist[i][j] > dist[i][k] + dist[k][j])
  34.                     dist[i][j]=dist[i][k]+dist[k][j];
  35.             }
  36.         }
  37.     }
  38.     printShortestPath(dist);
  39. }
  40.  
  41. int main(){
  42.     //Ma trạn ke duong di
  43.     //Duong di cua 1 diem toi chinh no la 0, duong di tu 1 diem i toi diem j ung voi graph[i][j],duong di chua khoi tao ung voi F
  44.     int graph[V][V] = { {0, 5, F, 10},
  45.                         {F, 0, 3, F},
  46.                         {F, F, 0, 1},
  47.                         {F, F, F, 0}
  48.                     };
  49.     Floyd(graph);
  50. }
  51.  
  52.  
  53. ---coin collect
  54. #include <iostream>
  55. #define n 5
  56. #define m 6
  57. using namespace std;
  58. void trace(int dp[][m]){
  59.     int i=n-1,j=m-1;
  60.     int maptrace[n][m];
  61.     for(int i=0;i<n;i++){
  62.         for(j=0;j<m;j++){
  63.             maptrace[i][j]=0;
  64.         }
  65.     }
  66.  
  67.     while (i>0 || j>0){
  68.         if(i==0 && j>0){
  69.             maptrace[i][j-1]=1;
  70.             j--;
  71.         }
  72.         else if(j==0 && i>0){
  73.             maptrace[i-1][j]=1;
  74.             i--;
  75.         }
  76.         else if(dp[i][j-1]<dp[i-1][j]){
  77.             maptrace[i-1][j]=1;
  78.             i--;
  79.         }
  80.         else{
  81.             maptrace[i][j-1]=1;
  82.             j--;
  83.         }
  84.     }
  85.     cout<<"...........................................\n";
  86.     cout<<"Duong di cua robot: \n";
  87.     for (i = 0; i < n; i++)
  88.     {
  89.         for (j = 0; j < m; j++)
  90.         {
  91.                 cout<<maptrace[i][j]<<"\t";
  92.         }
  93.         cout<<"\n";
  94.     }
  95. }
  96. void coinCollect(int graph[][m]){
  97.     int dp[n][m],i,j;
  98.     for(int i=0;i<n;i++){
  99.         for(j=0;j<m;j++){
  100.             dp[i][j]=0;
  101.         }
  102.     }
  103.  
  104.     for(i=0;i<n;i++){
  105.         for(j=0;j<m;j++){
  106.             if(graph[i][j]==1){
  107.                 if(i==0&&j==0)
  108.                     dp[i][j]=1;
  109.                 else if (i - 1 < 0)
  110.                         dp[i][j] = dp[i][j - 1] + 1;
  111.                 else if (j - 1 < 0)
  112.                         dp[i][j] = dp[i - 1][j] + 1;
  113.                 else
  114.                         dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + 1;
  115.             }
  116.             else
  117.                 {
  118.                     if (i == 0 && j == 0) dp[i][j] = 0;
  119.                     else if (i - 1 < 0)
  120.                         dp[i][j] = dp[i][j - 1];
  121.                     else if (j - 1 < 0)
  122.                         dp[i][j] = dp[i - 1][j];
  123.                     else
  124.                         dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
  125.                 }
  126.         }
  127.     }
  128.     cout<<"Bang trong so duong di cua robot:\n";
  129.     for (i = 0; i < n; i++)
  130.     {
  131.         for (j = 0; j < m; j++)
  132.         {
  133.                 cout<<dp[i][j]<<"\t";
  134.         }
  135.         cout<<"\n";
  136.     }
  137.     trace(dp);
  138. }
  139.  
  140. int main(){
  141.     int graph[n][m]={{0,0,0,0,1,0},
  142.                       {0,1,0,1,0,0},
  143.                       {0,0,0,1,0,1},
  144.                       {0,0,1,0,0,1},
  145.                       {1,0,0,0,1,0}};
  146.     coinCollect(graph);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement