wap 机试(lsp) 问题 暴力法

16 篇文章 0 订阅
#include <iostream>
#include <queue>
#include <assert.h>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;

const int dx[] = { -1 , 0 , 0, 1};
const int dy[] = { 0 ,-1 ,1 , 0 }; //4 direction

struct Point{
	Point(){};
	Point(int x,int y):m_x(x),m_y(y){}
	int m_x;
	int m_y;
	bool operator == (const Point & rPoint ){
		return rPoint.m_x == m_x && rPoint.m_y == m_y;
	}
};

class Orienteering{
public: 
	int main();
	bool isValid(const Point & p){
		bool ans = p.m_x >=0 && p.m_x < cMap.size() && p.m_y >= 0 && p.m_y < cMap[0].size() && cMap[p.m_x][p.m_y] != '#' ;
		return ans;
	}
	bool findG(const Point &cPoint,const Point &ePoint){
		for(int i = 0 ; i < 4 ; i ++ ){
			Point p(cPoint.m_x + dx[i] , cPoint.m_y + dy[i]);
			if(isValid(p) && !bVisited[p.m_x][p.m_y] && p == ePoint){
				return true;  // 只考虑一个
			}
			if(isValid(p) && ! bVisited[p.m_x][p.m_y]){
				Qtemp.push(p);
				bVisited[p.m_x][p.m_y] = true;
			}
		}
		return false;
	}
	void ClearQueue(queue<Point> &Q){
		while(!Q.empty()){
			Q.pop();
		}
	}

	void ResetVisted(){
		for(int i = 0 ; i < bVisited.size() ; i ++)
			for(int j = 0 ; j < bVisited[0].size() ; j ++ )
				bVisited[i][j] = false;
	}


	int findShortest(const Point & sPoint,const Point &ePoint){
		ResetVisted();
		ClearQueue(Qin);
		ClearQueue(Qtemp);

		shortestCount = 0;

		Qin.push(sPoint);
		bVisited[sPoint.m_x][sPoint.m_y] = true;
		while (true){
			while(!Qin.empty()){
				Point cur = Qin.front();
				if(findG(cur,ePoint)){
					shortestCount += 1;
					return shortestCount;
				}
				Qin.pop();
			}
			if(Qtemp.empty())
				return -1;// no way to
			else{
				shortestCount += 1; 
				swap(Qtemp,Qin);
			}
		}
		return -1;
	}


	void permutation(vector<vector<int>> & perms,int *a,int n,int k)
	{
		if(k == n){
			vector<int> temp(a,a+n);
			perms.push_back(temp);
			return;
		}
		for(int i = k; i < n ; i ++ ){
			swap(a[i],a[k]);
			permutation(perms,a,n,k+1);
			swap(a[i],a[k]);
		}

	}


	void readFile(string & filePath,vector<string> &cMpas){
		ifstream in(filePath);
		if(!in.is_open())
			return;
		string line;
		getline(in,line);
		sscanf(line.c_str(),"%d %d",&m,&n);
		for(int i = 0 ; i < m ; i ++ ){

			getline(in,line);
			cMpas.emplace_back(line);
		}
	}

	int m,n;
	int shortestCount;
	vector<vector<bool>> bVisited;
	queue<Point> Qin;
	queue<Point> Qtemp;
	vector<string> cMap;
};

int Orienteering::main(){
	/*
	int mapBackup[M][N];
	memcpy(mapBackup,path,sizeof(path)/sizeof(int));*/
	string filePath = "example1.txt";
	readFile(filePath,cMap);
	bVisited.resize(m,vector<bool>(n,false));

	vector<Point> ats;
	Point pStart;
	Point pEnd;

	for(int i = 0 ; i < m ; i ++ ){
		for(int j = 0 ; j < n ; j ++ ){
			if(cMap[i][j] == '@')
				ats.push_back(Point(i,j));
			if(cMap[i][j] == 'S')
				pStart = Point(i,j);
			if(cMap[i][j] == 'G')
				pEnd = Point(i,j);
		}
	}
	int gNums = ats.size();
	int *arrayNums = new int[gNums]; 
	for(int i = 0 ; i < gNums ; ++ i)
		arrayNums[i] = i;
	vector<vector<int>> perms;
	permutation(perms,arrayNums,gNums,0);
	int minLen = 100000;
	for(int i = 0 ; i < perms.size() ; i ++ ){
		int totalLen = 0;
		Point endP = ats[perms[i][0]];
		int len = findShortest(pStart,endP);
		if(len ==-1)
			return -1;
		totalLen += len;
		for(int j = 0 ; j < perms[i].size() -1 ; j ++ )
		{
			Point startPoint = ats[perms[i][j]];
			Point endPoint = ats[perms[i][j + 1]];
			int len = findShortest(startPoint,endPoint);
			if(len == -1)
				return -1;
			totalLen += len;
		}
		len = findShortest(ats[perms[i][perms[i].size()-1]],pEnd);
		if(len == -1)
			return -1;
		totalLen += len;
		minLen = min(totalLen,minLen);
	}
	delete[] arrayNums;
	return minLen;
}




int main(){
	Orienteering o;
	int ans = o.main();
	system("pause");
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值