(挑战编程_13_1)Dog and Gopher

http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=111301&format=html

#include <iostream>
#include <cstdlib>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <fstream>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <sstream>
#include <queue>
#include <hash_map>


using namespace std;

//#define _MY_DEBUG_

const int MAX_PORT_NUM = 1001;

long long ports[MAX_PORT_NUM][2];


/*
	MainStructure: 主结构
*/
void MainStructure();

/*
	Process
*/
void Process();

bool IsGhohperCanEscape(long long ghX, long long ghY, 
	long long dogX, long long dogY, int index);


int main()
{
	MainStructure();
	return 0;
}


void MainStructure()
{
	
#ifdef _MY_DEBUG_
	string inputStr = "C:/Users/cao/Desktop/算法/要看的书/算法/2挑战编程/input/1.inp";
	//string inputStr = "D:/2挑战编程/input/110205.inp";
	string outputStr = "C:/Users/cao/Desktop/算法/要看的书/算法/2挑战编程/input/1.outp";
	ifstream fin;
	fin.open(inputStr);		

	cin.rdbuf(fin.rdbuf());	

	/*
	ofstream fout;
	fout.open(outputStr.c_str());
	cout.rdbuf(fout.rdbuf());
	*/

#endif

	Process();

}
void Process()
{
	string str;
	while (getline(cin, str))
	{
		if (str == "")
		{
			break;
		}
		int portNum;
		long long gopherX, gopherY;
		long long dogX, dogY;
		stringstream ss(str);
		double tmpX, tmpY;
		ss >> portNum >> tmpX >> tmpY;
		gopherX = (long long)(tmpX * 1000);
		gopherY = (long long)(tmpY * 1000);
		ss >> tmpX >> tmpY;
		dogX = (long long)(tmpX * 1000);
		dogY = (long long)(tmpY * 1000);

		for (int i = 0; i < portNum; ++i)
		{
			cin >> tmpX >> tmpY;
			ports[i][0] = (long long)(tmpX * 1000);
			ports[i][1] = (long long)(tmpY * 1000);
		}
		cin.ignore();
		cin.ignore();

		bool isEscape = false;
		for (int i = 0; i < portNum; ++i)
		{
			if (IsGhohperCanEscape(gopherX, gopherY, dogX, dogY, i))
			{
				double ansX = ((double)ports[i][0]) / 1000;
				double ansY = ((double)ports[i][1]) / 1000;
				printf("The gopher can escape through the hole at (%.3lf,%.3lf).\n", ansX, ansY);	
				isEscape = true;
				break;
			}			
		}
		if (!isEscape)
		{
			cout << "The gopher cannot escape." << endl;
		}
	}
}

bool IsGhohperCanEscape(long long ghX, long long ghY, long long dogX, 
	long long dogY, int index)
{
	long long portX = ports[index][0];
	long long portY = ports[index][1];

	long long ghLen = (ghX - portX) * (ghX - portX) + (ghY - portY) * (ghY - portY);
	long long dogLen = (dogX - portX) * (dogX - portX) + (dogY - portY) * (dogY - portY);

	if (4 * ghLen <= dogLen)
	{
		return true;
	}
	return false;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值