I.Gree的心房(思维题)

I.Gree的心房(思维题)_第1张图片
I.Gree的心房(思维题)_第2张图片
一开始我读到这道题以为要用dfs结果,读完之后发现好像就是一个思维题;
分析:
首先应该知道因为题目说了,一个人只能走四个方向(up,down,left,right);所以从左上角到右下角,那么就是都走直线才能最短;因为障碍物是随便放的,所以我发现只需要讨论障碍物个数就可以了:
I.Gree的心房(思维题)_第3张图片
这样看了这个图,就会知道如果K的值>(n-1)*(m-1)个那么就可知肯定不可能走到最右下角;
所以答案就出来了:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
#define Max 0x3f3f3f3f
//ll gcd(ll a,ll b){
//	 return b?gcd(b,a%b):a;
//}
//ll QM(ll x,ll n){
//	  ll res=1;
//	  while(n){
//	  	  if(n&1){
//	  	  	  res=(res*x)%Mod;
//			}
//			x=(x*x)%Mod;
//			n>>=1;
//	  }
//}
int main(){
	ll n,m,k;
	cin>>n>>m>>k;
	if(k>(n-1)*(m-1)) printf("-1\n");
	else printf("%lld\n",n+m-2); //因为这里重了一个并且开头不算,所以需要减去2
	return 0;
	
} 

你可能感兴趣的:(I.Gree的心房(思维题))