topcoder AvoidRoads solution

来源:互联网 发布:java paxos算法 编辑:程序博客网 时间:2024/06/06 06:58

说了好久,写写自己的学习笔记,既算是备份,亦算作平时的总结。终于没多少课程了,也可以多多做做topcoder算法题了。 希望自己能一直坚持下去,也欢迎能和大家一起学习!问题描述等,均是根据自己理解翻译,若有歧义,可查看topcoder官网。

问题描述:在城市中,道路是以网格形式的方式布置的,网格的4个顶点,代表相应的街区的道路在此相交。当建立起坐标系后,每个交汇点均有唯一的坐标与之对应。

如下图所示:



问题:现在需求出从点(0,0)到点(width,height)的恰好经过widht + height步的方案的总数。

补充说明:标记为bad的路径不能通过(选择路径时,不能经过该路径)。形式类似bad ={"a b c d","e f gh"}

此处则表示(a,b)与(c,d)不可达,(e,f)与(g,h)不可达,不可达的路径数目可以有很多。

限制条件:

1.a,b,c,d为整数,无前导零(即第一位即为有效位,如a为8而不是08);

2.0<= a,c<=width, 0<=b,d<=height;

3.

测试数据(分别为widht,height,bad):

1.

输入:6,6,{"0 0 0 1","6 6 5 6"}

输出:252

2.

输入:1,1,{}

输出:2

3.

输入:35,31,{}

输出:6406484391866534976

4.

输入:2,2,{"0 0 1 0", "1 2 2 2", "1 1 2 1"}

输出:0

解题思路:

对于目标点(width,height)-一下简计为dest,分一下几种情况:

1.dest为边界点,即width,height两者中至少有一个为0;

①两者同时为0,即dest = (0,0),显然output = 0; 

②width为0(即在坐标系的y轴上),此时如果从(0,0)->(0,height)中不存在不可达的路径,则output = 1

否则,output = 0;

③height = 0,如果从(0,0)->(width,0)中不存在不可达的路径,则output = 1,否则output = 0.

2.dest不为边界点:

①(width,height-1)->(width,height)不可达 且(width-1,height)->(width,height)可达,output = Metrix[width-1,height]//Metrix[i][j]存///放(0,0)到(i,j)的总路径数目(去除了不可能的路径情况);

(width,height-1)->(width,height)可达 且(width-1,height)->(width,height)不可达,output = Metrix[width,height -1];

(width,height-1)->(width,height)可达 且(width-1,height)->(width,height)可达,output = Metrix[width -1][height] + Metrix[width][height -1];

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

#include <string>#include <iostream>#include <vector>using namespace std;class AvoidRoads{public:AvoidRoads(int width,int height,vector<string> bad){long i;row = width;Metrix = (double**)new double[width + 1];for(i = 0 ;i< width + 1;i++){Metrix[i] = (double*)new double[height + 1];//分配间空memset(Metrix[i],0,(height + 1)*sizeof(double));} Extract_Bad(bad);}double numWays (int width,int height,vector<string> bad){if(width < 1 || height <1)return -1;//errorways(width,height);return Metrix[width][height];}void ways(long width,long height){int pos_y,pos_x;bool flag_left = false,flag_down =false;for(pos_y = 0 ;pos_y<height+1 ;pos_y++)for(pos_x = 0 ;pos_x<width+1;pos_x++){flag_down = false;flag_left = false;for(int k = 0 ;k<BadSize;k++){if(BadPath[k][0] == pos_x&&BadPath[k][1] ==pos_y){if((BadPath[k][2] == pos_x-1)&&(BadPath[k][3] == pos_y))flag_left = true;else if((BadPath[k][2] == pos_x)&&(BadPath[k][3] == pos_y-1)){flag_down = true;}}if(flag_down == true && flag_left == true)break;}//边界上点if(pos_x == 0 && pos_y == 0)Metrix[0][0] = 0;else if(pos_x == 0 &&pos_y != 0){if(flag_down == true)Metrix[0][pos_y] = 0;else {if(pos_y == 1)//直接取值为1,不能取前一个点路径的数目,因Metrix[0][0]=0Metrix[0][1] =1;elseMetrix[0][pos_y] = Metrix[0][pos_y -1];}}else if(pos_y ==0 && pos_x != 0){if(flag_left == true)Metrix[pos_x][0] =0;else{if(pos_x == 1)//直接取值为1,不能去前一个点路径的数目,因Metrix[0][0]=0Metrix[1][0] =1;elseMetrix[pos_x][0] = Metrix[pos_x - 1][0];}}else //非边界{if(flag_down == true && flag_left != true)Metrix[pos_x][pos_y]= Metrix[pos_x -1][pos_y];else if(flag_left == true && flag_down != true)Metrix[pos_x][pos_y]= Metrix[pos_x][pos_y - 1];else if(flag_down == true && flag_left == true)Metrix[pos_x][pos_y]= 0;elseMetrix[pos_x][pos_y] = Metrix[pos_x -1][pos_y] + Metrix[pos_x][pos_y -1];}}}//分理出bad pathvoid Extract_Bad(vector<string> bad){BadSize = bad.size();int i;string line;long swap_x,swap_y;BadPath = (long**)new long[BadSize];for(i = 0 ;i< BadSize;i++){BadPath[i] = (long*)new long[4];int pos = bad[i].find_first_not_of(' ',0);BadPath[i][0] =atoi( &bad[i][pos]);pos = bad[i].find_first_not_of(' ',pos+1);BadPath[i][1] = atoi( &bad[i][pos]);pos = bad[i].find_first_not_of(' ',pos+1);BadPath[i][2] = atoi( &bad[i][pos]);pos = bad[i].find_first_not_of(' ',pos+1);BadPath[i][3] = atoi( &bad[i][pos]);if((BadPath[i][2] >BadPath[i][0]) || (BadPath[i][3] >BadPath[i][1])){swap_x = BadPath[i][2];swap_y = BadPath[i][3];BadPath[i][2] = BadPath[i][0];BadPath[i][3] = BadPath[i][1];BadPath[i][0] = swap_x;BadPath[i][1] = swap_y;}}}~AvoidRoads(){long i;for(i = 0 ;i< row +1;i++)delete[] Metrix[i];delete[] Metrix;for(i = 0 ;i< BadSize;i++)delete[] BadPath[i];delete[] BadPath;}private:double** Metrix;long** BadPath;long row,BadSize;};int _tmain(int argc, _TCHAR* argv[]){vector<string> bad;bad.push_back("0 0 0 1");bad.push_back("6 6 5 6");//测试数据AvoidRoads rod(6,6,bad);cout<<rod.numWays(6,6,bad)<<endl;return 0;}


0 0