ChessMetric - 2003 TCCC Round 4
来源:互联网 发布:淘宝摩托车专卖店 编辑:程序博客网 时间:2024/05/23 21:29
Problem Statement
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 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).
#include <stdlib.h>#include <string.h>#include <iostream>#define N 105#define M 55using namespace std;long long dp[N][N][M];int s;int d[16][2] = {0, 1, 0, -1, -1, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, 1, 2, -1, 2, 2, 1, -2, 1, 1, -2, -1, -2, 2, -1, -2, -1};int sr, sc;int DP(int r, int c, int n){ if(n == 0) { if(r == sr && c == sc) return 1; else return 0; } if(dp[r][c][n] != -1) return dp[r][c][n]; int an = 0; for(int i = 0; i < 16; i++) { int rr = r + d[i][0]; int cc = c + d[i][1]; if(rr >= 0 && rr < s && cc >= 0 && cc < s) { an += DP(rr, cc, n-1); } } dp[r][c][n] = an; return an;}long long howMany(int size, int start[], int end[], int numMoves){ sr = start[0]; sc = start[1]; s = size; memset(dp, -1, sizeof(dp)); return DP(end[0], end[1], numMoves);}
- ChessMetric - 2003 TCCC Round 4
- DP ChessMetric 【TCCC'03 Round 4】
- BadNeighbors - 2004 TCCC Round 4
- BadNeighbors (TCCC '04 Round 4 Div1 250)
- 拓扑排序 FlowerGarden - 2004 TCCC Round 1
- ZigZag - 2003 TCCC Semifinals 3
- TCCC Algo Qual Round 1(2007-08-19 02:59)
- TCCC Algo Qual Round (22007-08-21 20:51)
- TCCC Algo Round 1B(2007-08-30 20:54)
- 【TopCoder】ZigZag - 2003 TCCC Semifinals 3
- TopCoder-ChessMetric
- TopCoder ChessMetric
- TopCoder 250 points Tournaments 153-TCCC 07 Round 1C DIV 1 78.15/250 31.26%
- 2004 TCCC OL1 div1 1000(概率dp,简单)
- BestCoder Round #4 题解
- [BestCoder] Round #4
- BestCoder Round #4
- Codeforces Beta Round #4
- VS2008打开提示错误 There was a failure while initializing the Microsoft Visual SourceSafe
- 手把手教你做Android聊天机器人
- 混杂设备
- amfphp1.9和amfphp2.2,1差别
- JSP详细篇——JavaBean技术
- ChessMetric - 2003 TCCC Round 4
- 猜拳小游戏
- java 资源文件加载路径
- GDI绘制填充图一
- c++中的虚基类
- PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别
- C#中的Linq技术标准操作符大全(转)
- 第十三周项目2-成绩处理
- 学习GCD的一些基本用法