• 有两个好友A和B,住在一片长有蘑菇的由n*m个方格组成的草地,A在(1,1),B在(n,m)。现在A想要拜访B,由于她只想去B的家,所以每次她只会走(i,j+1)或(i+1,j)这样的路线,在草地上有k个蘑菇种在格子里(多个蘑菇可能在同一方格),问:A如果每一步随机选择的话(若她在边界上,则只有一种选择),那么她不碰到蘑菇走到B的家的概率是多少?


    第二种方法:首先分析题意,可用概率的方法来计算,做了好几道百度的题目,觉得大多数是再考概率论,所以首先要弄懂题意,最后做题前把公式写出来,这样编码时才能游刃有余。
    本题中下面的第一种用迭代枚举的方法来做是不对的,仅做错误示范

    // ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
    //
    
    #include<iostream>
    #include<vector>
    #include<iomanip>
    using namespace std;
    
    int main()
    {
    	int M, N, K;
    
    	while (cin >> N >> M >> K)
    	{
    		vector<vector<int>> lawn(N, vector<int>(M, 0));
    		vector<vector<float>> pVec(N, vector<float>(M, 0));
    		pVec[0][0] = 1;
    		for (int i = 0; i < K; i++)
    		{
    			int x, y;
    			cin >> x >> y;
    			lawn[x - 1][y - 1] = 1;
    		}
    		pVec[0][0] = 1;
    		for (int x = 0; x < N; x++)
    		{
    			for (int y = 0; y < M; y++)
    			{
    				if (x == 0 && y == 0)
    				{
    					//		cout << "x:" << x << "y:" << y << " " << "初始化" << endl;
    					pVec[0][0] = 1.0;
    	
    				}
    			   else if (lawn[x][y] == 1)//中间有障碍物
    				{
    				//	cout << "x:" << x << "y:" << y << "有障碍 "  << endl;
    					pVec[x][y] = 0;
    				}
    				
    				else if (x < lawn.size() - 1 && y < lawn[0].size() - 1) //中间
    				{
    				//	cout <<"x:"<<x<<"  y:"<<y<<" " <<"中间" << endl;
    
    					if (x - 1 >= 0)
    					{
    				//		cout << "#####" << endl;
    						pVec[x][y] = pVec[x][y] + pVec[x - 1][y] * 0.5;
    				//		cout << "x-1:" << x-1 << "  y:" << y << " " << pVec[x-1][y] << endl;
    				//		cout << "x:" << x << "  y:" << y << " " << pVec[x][y] << endl;
    					}
    					if (y - 1 >= 0)
    					{
    						pVec[x][y] = pVec[x][y] + pVec[x][y - 1] * 0.5;
    					}
    				}
    				else if (x < lawn.size() - 1 && y == lawn[0].size() - 1) //最右面
    				{
    		//			cout << "x:" << x << "y:" << y << " " << "最右面" << endl;
    					if (x - 1 >= 0)
    					{
    						pVec[x][y] = pVec[x][y] + pVec[x - 1][y] * 1;
    					}
    					if (y - 1 >= 0)
    					{
    						pVec[x][y] = pVec[x][y] + pVec[x][y - 1] * 0.5;
    					}
    				}
    				else if (x == lawn.size() - 1 && y < lawn[0].size() - 1) //最下面
    				{
    			//		cout << "x:" << x << "y:" << y << " " << "最下面" << endl;
    					if (x - 1 >= 0)
    					{
    						pVec[x][y] = pVec[x][y] + pVec[x - 1][y] * 0.5;
    					}
    					if (y - 1 >= 0)
    					{
    						pVec[x][y] = pVec[x][y] + pVec[x][y - 1] * 1;
    					}
    				}
    				else  //右下角
    				{
    		//			cout << "x:" << x << "y:" << y << " " << "右下角" << endl;
    					if (x - 1 >= 0)
    					{
    						pVec[x][y] = pVec[x][y] + pVec[x - 1][y] * 1;
    						
    					}
    					if (y - 1 >= 0)
    					{
    						pVec[x][y] = pVec[x][y] + pVec[x][y - 1] * 1;
    						//cout << "下面" << endl;
    					}
    				}
    
    			}
    
    		}
    		/*for (int i = 0; i < pVec.size(); i++)
    		{
    			for (int j = 0; j < pVec[0].size(); j++)
    			{
    				cout << pVec[i][j] << "  ";
    			}
    		}*/
    	//	cout << endl;
    		cout << setiosflags(ios::fixed);
    		cout << setprecision(2)<<pVec[N-1][M-1] << endl;
    
    	}
    	return 0;
    }
    

    第一种方法:迭代,枚举每条路径,但是当n或m大于10时算法就很慢了,这种方法不适用。当数大于等于9时,考虑使用第二种方法,和这个题目类似的是,京东出的题,小东发年终奖,但是那个题目是在6*6的棋盘上,不用担心算法复杂度,对这个题目不适合。。。。。。。

    #include "stdafx.h"
    #include<iostream>
    #include<vector>
    #include<iomanip>
    using namespace std;
    class Solution {
    public:
    	//没有障碍
    
    	int noObstacle(vector<vector<int>> lawn)
    	{
    
    		if (lawn.size() == 1)
    		{
    
    			return 1;
    		}
    		else if (lawn[0].size() == 1)
    		{
    
    			return 1;
    		}
    		else
    		{
    
    			return  deleteRow(lawn) + deleteCol(lawn);
    		}
    
    	}
    
    	int  deleteRow(vector<vector<int>> lawn)
    	{
    		lawn.erase(lawn.begin());
    		return	 noObstacle(lawn);
    	}
    
    	int  deleteCol(vector<vector<int>> lawn)
    	{
    
    		for (int i = 0; i < lawn.size(); i++)
    		{
    			lawn[i].erase(lawn[i].begin());
    		}
    		return	noObstacle(lawn);
    	}
    
    	//有障碍
    
    	int hasObstacle(vector<vector<int>> lawn)
    	{
    		if (lawn.size() == 1)
    		{
    			bool flag = false;
    			for (int i = 0; i < lawn[0].size(); i++)
    			{
    				if (lawn[0][i] != 0)
    				{
    					flag = true;
    					break;
    				}
    			}
    			if (flag == false) return 0;
    			return 1;
    		}
    		else if (lawn[0].size() == 1)
    		{
    			bool flag = false;
    			for (int i = 0; i < lawn.size(); i++)
    			{
    				if (lawn[i][0] != 0)
    				{
    					flag = true;
    					break;
    				}
    			}
    			if (flag == false) return 0;
    			return 1;
    		}
    		else
    		{
    
    			return  deleteRow2(lawn) + deleteCol2(lawn);
    		}
    	}
    	int  deleteRow2(vector<vector<int>> lawn)
    	{
    		if (lawn[1][0] != 0)
    		{
    			lawn.erase(lawn.begin());
    			return	 noObstacle(lawn);
    			return 0;
    		}
    		else
    		{
    			lawn.erase(lawn.begin());
    			return	 hasObstacle(lawn);
    		}
    		
    	}
    
    	int  deleteCol2(vector<vector<int>> lawn)
    	{
    		if (lawn[0][1] != 0)
    		{
    			for (int i = 0; i < lawn.size(); i++)
    			{
    				lawn[i].erase(lawn[i].begin());
    			}
    			return	 noObstacle(lawn);
    			return 0;
    		}
    		else
    		{
    			for (int i = 0; i < lawn.size(); i++)
    			{
    				lawn[i].erase(lawn[i].begin());
    			}
    			return	 hasObstacle(lawn);
    		}
    
    		
    		
    	}
    
    
    
    };
    
    int main()
    {
    	int M, N, K;
    	Solution so;
    	while (cin>>N>>M>>K)
    	{
    		vector<vector<int>> lawn;
    		for (int i = 0; i < N; i++)
    		{
    			vector<int> vec;
    			for (int j = 0; j < M; j++)
    			{
    				vec.push_back(0);
    			}
    			lawn.push_back(vec);
    		}
    	
    		for (int i = 0; i < K; i++)
    		{
    			int x,y;
    			cin >> x >> y;
    			lawn[x - 1][y - 1]= lawn[x - 1][y - 1]+1;
    		}
    	
    		cout << setiosflags(ios::fixed);
    		cout << "有障碍:" << so.hasObstacle(lawn)<<endl;
    		cout << "无障碍:" << so.noObstacle(lawn)  << endl;
    		
    	
    		float p = 1- ((float)so.hasObstacle(lawn)) / ((float)so.noObstacle(lawn));
    	
    		cout << setprecision(2)<< p << endl;
    	}
    	
    
    	return 0;
    }
  • 相关阅读:
    linux 下载openssl 亲测有效
    在linux文件中 怎么查看文件的行数
    linux 安装mysql 数据库
    bccomp
    javascript之date
    javascript之函数
    JavaScript数据类型之数组
    BOM
    canvas
    javascript数据类型之对象
  • 原文地址:https://www.cnblogs.com/wdan2016/p/6475371.html
Copyright © 2020-2023  润新知