Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #define endl '\n'
- using namespace std;
- int n, m, k;
- unsigned long int grid[2505][2505];
- bool valid[2505][2505];
- int blaster[2505][4];
- void makeTrue(){
- for(int i=0; i<=n; i++)
- for(int j=0; j<=m; j++)
- valid[i][j]=true;
- }
- //checks the state of every vertex at time t and changes it accordingly(iff its reachable at that instant)
- void stateValidator(int t){
- //makeTrue();
- for(int i=0; i<k; i++){
- int x=blaster[i][0], y=blaster[i][1], ts=blaster[i][2], f=blaster[i][3];
- while(t-ts>=0){
- //condition below checks the range and if that particular pulse is reachable at that instant
- if(x+(t-ts)<n && (x+(t-ts)+y-2-t)%f==0)valid[x+(t-ts)][y]=false;
- if(x-(t-ts)>=0 && (x-(t-ts)+y-2-t)%f==0)valid[x-(t-ts)][y]=false;
- if(y+(t-ts)<m && (y+(t-ts)+x-2-t)%f==0)valid[x][y+(t-ts)]=false;
- if(y-(t-ts)>=0 && (y+(t-ts)+x-2-t)%f==0)valid[x][y-(t-ts)]=false;
- ts+=f;
- }
- }
- }
- //runs the stateValidator for the (n+m) required times
- void finalState(){
- for(int i=1; i<=n; i++){
- stateValidator(i-1);
- }
- int i=n;
- while(i<=n){
- for(int j=1; j<=m; j++){
- stateValidator(i+j-2);
- }
- i++;
- }
- }
- //checks if its possible for the spiff to reach his final destination(vertex)
- bool possible(){
- //unsigned long int grid[n+1][m+1];
- for(int i=0; i<=n; i++)
- for(int j=0; j<=m; j++)grid[i][j]=0;
- grid[1][1]=1;
- for(int i=1; i <= n; i++){
- for(int j=1; j<=m; j++){
- //stateValidator(i+j-2);
- if(valid[i][j]){
- grid[i][j]+=grid[i-1][j]+grid[i][j-1];
- }else
- grid[i][j]=0;
- }
- }
- if(grid[n][m]!=0){
- return true;
- }else return false;
- }
- int main(){
- std::ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
- cin >> n >> m >> k;
- for(int i=0; i<k; i++){
- cin >> blaster[i][0] >> blaster[i][1] >> blaster[i][2] >> blaster[i][3];
- }
- makeTrue();
- finalState();//converts it essentially into a basic some vertex blocked path finder problem
- if(possible()){
- cout << "YES" << endl;
- cout << n+m-2 << endl;
- }else cout << "NO" << endl;
- }
Add Comment
Please, Sign In to add comment