Guest User

SaveSpaceManSpiffZCO

a guest
Oct 26th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. #define endl '\n'
  4.  
  5. using namespace std;
  6.  
  7. int n, m, k;
  8. unsigned long int grid[2505][2505];
  9. bool valid[2505][2505];
  10. int blaster[2505][4];
  11. void makeTrue(){
  12.     for(int i=0; i<=n; i++)
  13.         for(int j=0; j<=m; j++)
  14.             valid[i][j]=true;
  15. }
  16. //checks the state of every vertex at time t and changes it accordingly(iff its reachable at that instant)
  17. void stateValidator(int t){
  18.     //makeTrue();
  19.     for(int i=0; i<k; i++){
  20.         int x=blaster[i][0], y=blaster[i][1], ts=blaster[i][2], f=blaster[i][3];
  21.         while(t-ts>=0){
  22.             //condition below checks the range and if that particular pulse is reachable at that instant
  23.             if(x+(t-ts)<n && (x+(t-ts)+y-2-t)%f==0)valid[x+(t-ts)][y]=false;
  24.             if(x-(t-ts)>=0 && (x-(t-ts)+y-2-t)%f==0)valid[x-(t-ts)][y]=false;
  25.             if(y+(t-ts)<m && (y+(t-ts)+x-2-t)%f==0)valid[x][y+(t-ts)]=false;
  26.             if(y-(t-ts)>=0 && (y+(t-ts)+x-2-t)%f==0)valid[x][y-(t-ts)]=false;
  27.             ts+=f;
  28.         }
  29.     }
  30. }
  31. //runs the stateValidator for the (n+m) required times
  32. void finalState(){
  33.     for(int i=1; i<=n; i++){
  34.         stateValidator(i-1);
  35.     }
  36.     int i=n;
  37.     while(i<=n){
  38.         for(int j=1; j<=m; j++){
  39.             stateValidator(i+j-2);
  40.         }
  41.         i++;
  42.     }
  43. }
  44. //checks if its possible for the spiff to reach his final destination(vertex)
  45. bool possible(){
  46.     //unsigned long int grid[n+1][m+1];
  47.     for(int i=0; i<=n; i++)
  48.         for(int j=0; j<=m; j++)grid[i][j]=0;
  49.     grid[1][1]=1;
  50.     for(int i=1; i <= n; i++){
  51.         for(int j=1; j<=m; j++){
  52.             //stateValidator(i+j-2);
  53.             if(valid[i][j]){
  54.                 grid[i][j]+=grid[i-1][j]+grid[i][j-1];
  55.             }else
  56.                 grid[i][j]=0;
  57.         }
  58.     }
  59.  
  60.     if(grid[n][m]!=0){
  61.         return true;
  62.     }else return false;
  63. }
  64.  
  65. int main(){
  66.     std::ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
  67.     cin >> n >> m >> k;
  68.     for(int i=0; i<k; i++){
  69.         cin >> blaster[i][0] >> blaster[i][1] >> blaster[i][2] >> blaster[i][3];
  70.     }
  71.     makeTrue();
  72.     finalState();//converts it essentially into a basic some vertex blocked path finder problem
  73.     if(possible()){
  74.         cout << "YES" << endl;
  75.         cout << n+m-2 << endl;
  76.     }else cout << "NO" << endl;
  77. }
Add Comment
Please, Sign In to add comment