heisenberg0820

Untitled

Jun 6th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define wt while(t--)
  4. #define fast ios_base::sync_with_stdio(false)
  5. #define pb push_back
  6. #define pob pop_back
  7. #define pf push_front
  8. #define pof pop_front
  9. #define mp make_pair
  10. #define INF -1000000000000000
  11.  
  12. typedef unsigned long long int ull;
  13. typedef long long int ll;
  14. using namespace std;
  15.  
  16. ll b[505][505];
  17. ll p[505][505];
  18. ll dp[505][505];
  19. bool visited[505][505];
  20. ll n;
  21.  
  22. bool isSafe(ll i,ll j)
  23. {
  24. if(i>0&&i<=n&&j>0&&j<=n)
  25. return true;
  26. return false;
  27. }
  28.  
  29. ll recur(ll i,ll j)
  30. {
  31. if(dp[i][j]!=-1)
  32. return dp[i][j];
  33. if(visited[i][j]==true)
  34. return 0;
  35. else
  36. {
  37. visited[i][j]=true;
  38. if(i==n&&j==n)
  39. return dp[i][j]=b[i][j];
  40. ll k = b[i][j];
  41. ll a,c;
  42. a = c = INF;
  43. if(isSafe(i+1,j))
  44. {
  45. if(p[i+1][j]==1)
  46. a = recur(i+1,j);
  47. }
  48. if(isSafe(i,j+1))
  49. {
  50. if(p[i][j+1]==1)
  51. c = recur(i,j+1);
  52. }
  53. return dp[i][j]=k+max(a,c);
  54. }
  55. }
  56.  
  57.  
  58. int main()
  59. {
  60. fast;
  61. memset(p,0,sizeof(p));
  62. memset(dp,-1,sizeof(dp));
  63. memset(visited,false,sizeof(visited));
  64. ll q,i,j,k,l,r,x,ans;
  65. cin>>n>>q;
  66. for(i=1;i<=n;i++)
  67. {
  68. for(j=1;j<=n;j++)
  69. cin>>b[i][j];
  70. }
  71. x = 1;
  72. while(q--)
  73. {
  74. cin>>i>>j>>k;
  75. for(l = max(x,i-k);l<=min(i+k,n);l++)
  76. {
  77. for(r = max(x,j-k);r<=min(j+k,n);r++)
  78. {
  79. if(abs(l-i)+abs(r-j)<=k)
  80. p[l][r]=1;
  81. }
  82. }
  83. }
  84. ans = recur(1,1);
  85. if(visited[n][n]==true)
  86. {
  87. cout<<"YES\n";
  88. cout<<ans<<endl;
  89. }
  90. else
  91. cout<<"NO\n";
  92. return 0;
  93. }
Add Comment
Please, Sign In to add comment