1、2、 LELE的RPG难题:有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green) 三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也 不同色.编程全部的满足要求的涂法.#in cludeif(n=l) return 3;else if(n二=2) return 6;else return f(n-l)+f(n-2)*2;cinf( n)v 3、北大 ACM(1942)Paths on a GridTime Limit: 1000MS Memory Limit: 30000KDescriptionImagine you are attending your m
2、ath lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time he*s explaining that (a+b)2=a2+2ab+b2) So you decide to waste your time with drawing modern art instead.Fortunately you have a piece of squared paper and you choose a rect
3、angle of size n*m on the paper. Lets call this rectangle together with the lines it contains a grid. Starting at the lower left corner of the grid, you move your pencil to the upper right corner, taking care that it stays on the lines and moves only to the right or up. The result is shown on the lef
4、t:Really a masterpiece, isn*t it? Repeating the procedure one more time, you arrive with the picture shown on the right. Now you wonder: how many different works of art can you produce?InputThe input contains several testcases. Each is specified by two unsigned 32-bit integers n and m, denoting the
5、size of the rectangle. As you can observe, the number of lines of the corresponding grid is one more in each dimenision. Input is terminated by n=m=0.OutputFor each test case output on a line the number of different art works that can be generated using the procedure described above. That is, how ma
6、ny paths are there on a grid where each step of the path consists of moving one unit to the right or one unit up? You may safely assume that this number fits into a 32-bit unsigned integer.Sample Input5 41 10 0Sample Output1262#includeusing namespace std;long long f(long long m, long long n)if(n=O)
7、return 1; else return f(m-l,n-l)*m/n;int main()long long m,n;while(scanf(%l64d %l64d,&n,&m) & n+m) printf(%l64dnzf(m+n,min(mzn);return 0;1、(并查集)若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易, 现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚。如果x,y是亲戚,那么 x的亲戚都是y的亲戚,y的亲戚也都是x的亲戚。数据输入:第一行:三个整数(nv=5000,mv=5000,pv
8、=5000),分别表示有n个人,m个亲 戚关系,询问p对亲戚关系。以下m行:每行两个数Mi, Mj, l=Mi, Mjint fatherlOOOO;int find(int x)讦(fatherx=x) return x;elsereturn fatherx二find(fatherx);int n,m,p; scanf(%d%d%dn/&m,&p); for(int i=l;i二n;i+) fatheri=i;int a,b;for(int i=l;=m;i+)scanf(%d%da,&b); a=find(a);b=find(b);if(a!=b) fathera=b;=p;a=fi nd
9、(a);=b) printf(”Non“);YesnH); return 0;假设有两个皇后被放置在(i, j)和(k, 1)的位置上,明显,当且仅当 吋,两个皇后才在同一条对角线上。(1)先从首位开始检查,如果不能放置,接着检查该行第二个位置,依次检查 下去,直到在该行找到一个可以放置一个皇后的地方,然后保存当前状态,转到 下一行重复上述方法的检索。(2)如果检查了该行所有的位置均不能放置一个皇后,说明上一行皇后放置的 位置无法让所有的皇后找到自己合适的位置,因此就要回溯到上一行,重新检查 该皇后位置后面的位置。int PLACE (int n) /*检查当前列能否放置皇后*/queen i
10、 = queen n用于保证元素不能再冋一列/abs (queen i - queen n ) = abs (n - i)用于约束元索不能再同一行J4不能再同一 条斜线上int i;for (i = 0; i n; i + +) /*检查横排和对角线上是否可以放置皇后*/if(queen i = queenn | | abs(queen i - queenn) = abs(n - i)return 1; 1 2345678 9 10 11 12131415具体的实现代码如下:stdlibh#define max 4/sum用于描述解的可能的个数,每当输出一次复合要求的位置/sum的数量就会被+
11、1int queen max , sum=O; /* max 为棋盘最大坐标 */void show () /*输出所有皇后的坐标*/printf (n (n);/i代表行数,queen i代表当前行元素所处的列数,/注意此处下标是从0开始的 max; i+)printf ( n %d”,queeni+1);printf(”)n);/每次输出一种解的时候,那么他的解的数量就会增加1sum+;/此函数用于判断皇后当前皇后是否可以放在此位置/queen i = queen n用于保证元素不能再同一列/abs (queen i - queen n ) = abs (n - i)用于约束元素不能再同一
12、行且不能再同一 条斜线上 n; i+) /*检查横排和对和线上是否可以放置皇后*/if(queeni = queenn | | abs (queeni 一 queenn) = abs(n 一 i)/核心函数,冋溯法的思想void NQUEENSfint n) /*回溯尝试皇后位置小为横坐标*/首先将皇后放在第0列的位置,对于第一次來说是肯定成立的/所以第一次将皇后放在第0行0列的位置queen n = i; /*将皇后摆到当前循环到的位置*/if(PLACE(n)if (n = max 一 1)show(); /*如果全部摆好,则输出所有皇后的坐标*/NQUEENS (n + 1) ; /*否则继续摆放下一个皇后*/FF31KAHZD1H8ETZ8WWEZWUUVAint main ()NQUEENS (0) ; /*从横坐标为0开始依次尝试*/printf(nn H;print f
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1