求一道ACM练习题的算法解决方法
求一道ACM练习题的算法
题目原文如下:
Description
Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square-shaped chocolate cake with known size. She asks each invited person to determine the size of the piece of cake that he/she wants (which should also be square-shaped). She knows that Mr. Kavoosi would not bear any wasting of the cake. She wants to know whether she can make a square cake with that size that serves everybody exactly with the requested size, and without any waste.
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case. Each test case consist of a single line containing an integer s, the side of the cake, followed by an integer n (1 ≤ n ≤ 16), the number of cake pieces, followed by n integers (in the range 1..10) specifying the side of each piece.
Output
There should be one output line per test case containing one of the words KHOOOOB! or HUTUTU! depending on whether the cake can be cut into pieces of specified size without any waste or not.
Sample Input
2
4 8 1 1 1 1 1 3 1 1
5 6 3 3 2 1 1 1
Sample Output
KHOOOOB!
HUTUTU!
题目意思还是比较简单的,就是给定一个正方形的边长,要求分成指定边长、指定个数的小正方形,判断是否能恰好分完。
------解决方案--------------------
fun( int idx)
{
if (idx==n)
{
找到有解退出;
}
r=查找大正方形内有空格的最顶的行号;
c=在该行内查找最左边的空格的列号;
foreach(剩下的尚未放置的n-idx个小正方形)
{
k=当前小正方形的边长;
if(大正方形的(r,c)到 (r+k-1,c+k-1)的小区域内都是空格, 也就是该小个正方形能在(r,c)点放下)
{
放置该小个正方形;置大正方形的(r,c)到 (r+k-1,c+k-1)的单元格为非空格状态;
fun(idx+1);
拿起该小正方形,置大正方形的(r,c)到 (r+k-1,c+k-1)的单元格为空格状态;
}
}
}
调用方式:
置大正方形的所有单元格为空格状态;
fun(0);
题目原文如下:
Description
Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square-shaped chocolate cake with known size. She asks each invited person to determine the size of the piece of cake that he/she wants (which should also be square-shaped). She knows that Mr. Kavoosi would not bear any wasting of the cake. She wants to know whether she can make a square cake with that size that serves everybody exactly with the requested size, and without any waste.
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case. Each test case consist of a single line containing an integer s, the side of the cake, followed by an integer n (1 ≤ n ≤ 16), the number of cake pieces, followed by n integers (in the range 1..10) specifying the side of each piece.
Output
There should be one output line per test case containing one of the words KHOOOOB! or HUTUTU! depending on whether the cake can be cut into pieces of specified size without any waste or not.
Sample Input
2
4 8 1 1 1 1 1 3 1 1
5 6 3 3 2 1 1 1
Sample Output
KHOOOOB!
HUTUTU!
题目意思还是比较简单的,就是给定一个正方形的边长,要求分成指定边长、指定个数的小正方形,判断是否能恰好分完。
------解决方案--------------------
fun( int idx)
{
if (idx==n)
{
找到有解退出;
}
r=查找大正方形内有空格的最顶的行号;
c=在该行内查找最左边的空格的列号;
foreach(剩下的尚未放置的n-idx个小正方形)
{
k=当前小正方形的边长;
if(大正方形的(r,c)到 (r+k-1,c+k-1)的小区域内都是空格, 也就是该小个正方形能在(r,c)点放下)
{
放置该小个正方形;置大正方形的(r,c)到 (r+k-1,c+k-1)的单元格为非空格状态;
fun(idx+1);
拿起该小正方形,置大正方形的(r,c)到 (r+k-1,c+k-1)的单元格为空格状态;
}
}
}
调用方式:
置大正方形的所有单元格为空格状态;
fun(0);