DP中级

  • AvoidRoads - 2003 TCO Semifinals 4
  • 题目:
  •        就是从(0,0)到(n,m)的路径个数有多少个,但是会有一些路径是损坏的,要求路径总个数是n+m,这就要求只能向右走或者向上走,返回路径的个数。 

二维dp。
状态转移  dp[i][j]=dp[i-1][j]+dp[i][j]

>需要判断点的合法性
>判断边的合法性,如果不合法,从那个点不可达,即+0

/*************************************************************************
    > File Name: 2003tcos4.cpp
    > Author: cy
    > Mail: 1002@qq.com 
    > Created Time: 2014/8/25 15:59:45
 ************************************************************************/

#include<iostream>
#include<cstring>
#include <algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <sstream>

#define maxn 50000+5

#define inf 0x3f3f3f3f
  #define INF 0x3FFFFFFFFFFFFFFFLL
#define rep(i,n) for(i=0;i<n;i++)
 #define reP(i,n) for(i=1;i<=n;i++)

#define ull unsigned long long
 #define ll long long

#define cle(a) memset(a,0,sizeof(a))
using namespace std;
typedef pair<int,int >pi;
class AvoidRoads
{
	public:
	int n,m;
	struct edge{
		vector<pi>V;
		int num;
		void init()
		{
		    num=0;
			V.clear();
		}
	}E[105][105];
	int getval(char c){
		return c-'0';
	}
	int getbool(int x,int y){
		if(x<0||y<0)return false;
		if(x>n||y>m)return false;
		return true;
	}
    long long numWays(int width, int height, vector <string> bad)
    {
		n=width,m=height;
		int len=bad.size();
		int i,j,k;
		rep(i,len){
			string temp=bad[i];
			int x1,y1,x2,y2;
		    sscanf(bad[i].c_str(),"%d%d%d%d",&x1,&y1,&x2,&y2);
			pi p=make_pair(x2,y2);
			E[x1][y1].num++;
			E[x1][y1].V.push_back(p);
			p= make_pair(x1,y1);
			E[x2][y2].num++;
			E[x2][y2].V.push_back(p);
		}
		ll dp[105][105];
		cle(dp);
		dp[0][0]=1;
	    rep(i,n+1){
			rep(j,m+1){
				if(getbool(i-1,j)){
					int len=E[i][j].num;
					rep(k,len){
						if((E[i][j].V[k].first)==(i-1)&&(E[i][j].V[k].second)==(j))
						{
						    break;
						}
					}
					if(k==len)dp[i][j]+=dp[i-1][j];
				}
				if(getbool(i,j-1)){
					int len=E[i][j].num;
					rep(k,len){

						if(E[i][j].V[k].first==i&&E[i][j].V[k].second==(j-1))break;
					}
				   if(k==len)	dp[i][j]+=dp[i][j-1];
				}
			}
		}
		return dp[n][m];
    }
};
int main()
{
#ifndef ONLINE_JUDGE
   //  freopen("in.txt","r",stdin);
     //freopen("out.txt","w",stdout);
#endif
	int n,m;
	n=6;m=6;
	string s1="0 0 0 1";
	string s2="6 6 5 6";
	AvoidRoads a;
	vector<string>ss;
	ss.clear();
	ss.push_back(s1);
	ss.push_back(s2);
	cout<<a.numWays(6,6,ss)<<endl;
    return 0;
}

题目:
 
    
 Suppose you had an n by n chess board and a super piece called a kingknight. Using only one move the kingknight denoted 'K' below can reach any of the spaces denoted 'X' or 'L' below:
   .......
   ..L.L..
   .LXXXL.
   ..XKX..
   .LXXXL.
   ..L.L..
   .......
In other words, the kingknight can move either one space in any direction (vertical, horizontal or diagonally) or can make an 'L' shaped move. An 'L' shaped move involves moving 2 spaces horizontally then 1 space vertically or 2 spaces vertically then 1 space horizontally. In the drawing above, the 'L' shaped moves are marked with 'L's whereas the one space moves are marked with 'X's. In addition, a kingknight may never jump off the board.

Given the size of the board, the start position of the kingknight and the end position of the kingknight, your method will return how many possible ways there are of getting from start to end in exactly numMoves moves. start and finish are vector <int>s each containing 2 elements. The first element will be the (0-based) row position and the second will be the (0-based) column position. Rows and columns will increment down and to the right respectively. The board itself will have rows and columns ranging from 0 to size-1 inclusive.

Note, two ways of getting from start to end are distinct if their respective move sequences differ in any way. In addition, you are allowed to use spaces on the board (including start and finish) repeatedly during a particular path from start to finish. We will ensure that the total number of paths is less than or equal to 2^63-1 (the upper bound for a long long).
  
题意:一个棋子可以走相邻的八个方向和“L”型方向,给出棋牌大小和起点终点,给出步骤,一定要满足这个步骤,返回方案的数目。
解法:
          先看官方题解
The most popular way to solve this problem uses dynamic programming. First, set up a 2-dimensional array that represents the board. Each element of the array will signify how many ways there are of reaching a given square. In other words, after the ith iteration of our loop, a particular element in the array will represent how many ways there are of reaching the corresponding square in i moves. To initialize the array, all elements should be 0 except the start square which should be 1. We then loop over the number of moves, using the array from the previous iteration to produce the array for the next iteration.
二维数组代表的是,到达这个点的方案数目。初始化,全为0,起点为1,。一步一步就好了。
/*************************************************************************
    > File Name: 2003tccc4250.cpp
    > Author: cy
    > Mail: 1002@qq.com 
    > Created Time: 2014/8/25 18:30:17
 ************************************************************************/

#include<iostream>
#include<cstring>
#include <algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 50000+5

#define inf 0x3f3f3f3f
  #define INF 0x3FFFFFFFFFFFFFFFLL
#define rep(i,n) for(i=0;i<n;i++)
 #define reP(i,n) for(i=1;i<=n;i++)

#define ull unsigned long long
 #define ll long long

#define cle(a) memset(a,0,sizeof(a))

using namespace std;
 int dir[16][2]={-2,-1,-2,1,-1,-2,-1,-1,-1,0,-1,1,-1,2,0,-1,0,1,1,-2,1,-1,1,0,1,1,1,2,2,-1,2,1};
class ChessMetric
{
	public:
		int n;
		int getbool(int x,int y){
			if(x<0||y<0)return false;
			if(x>=n||y>=n)return false;
			return true;
		}
        long long howMany(int size, vector <int> start, vector <int> end, int numMoves)
       {
		       n=size;
               int x1,x2,y1,y2;
			   x1=start[0],y1=start[1],x2=end[0],y2=end[1];
			   ll dp[105][105];
			   cle(dp);
			   dp[x1][y1]=1;
			   ll tdp[105][105];
			   int i,j,k;
			   for(i=1;i<=numMoves;i++)
			   {
				   cle(tdp);
				   for(j=0;j<n;j++){
					   for(k=0;k<n;k++){
						   for(int ii=0;ii<16;ii++)
						   {
							   if(getbool(j+dir[ii][0],k+dir[ii][1]))tdp[j][k]+=dp[j+dir[ii][0]][k+dir[ii][1]];
						   }
					   }
				   }
				   rep(j,n){
					   rep(k,n){
						   dp[j][k]=tdp[j][k];
					   }
				   }
			   }
			   return dp[x2][y2];
       }
};

   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值