当前位置:网站首页>Bailian4006 little rabbit picks up gold coins [simulation]
Bailian4006 little rabbit picks up gold coins [simulation]
2022-04-21 21:21:00 【Island blog】
4006: The little rabbit picked up gold coins
Total time limit : 1000ms Memory limit : 65536kB
describe
In a game , The little rabbit can pick up gold coins . It will pick up the passing gold coins while walking . When the little rabbit found that there were no gold coins ahead , Will automatically turn right . Until all the gold coins were picked up . Now put one NХN In the square area of , Gold coins are placed in each grid . Suppose the little rabbit starts from the first space on the top left , Go to the right . Here is the following :
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
The second one I found k A gold coin will be marked k, It is recorded as the serial number of the gold coin . Then ask No i That's ok , The first j What is the serial number of the gold coins in the column ? (1≤i≤N, 1≤j≤N)
Input
The first line has an integer K and N, N Indicates the size of the square area (1≤N≤10000), and K Indicates the number of groups of input coordinates .(1≤K≤25)
And then there will be K That's ok , Each line has two integers separated by spaces i and j, Indicates the number of rows and columns of gold coins to be sought .
Output
Total output K That's ok . Each line outputs a serial number of gold coins in corresponding coordinates .
The sample input
3 4
1 3
2 2
3 3
Sample output
3
13
15
Question link :Bailian4006 The little rabbit picked up gold coins
A brief description of the problem :( A little )
Problem analysis : Simulation question , Don't explain .
Program description :( A little )
Reference link :( A little )
. :( A little )
AC Of C++ The language program is as follows :
/* Bailian4006 The little rabbit picked up gold coins */
#include <stdio.h>
#define N 10000
int c[N + 1];
int dx[4] = {
0, 1, 0, -1};
int dy[4] = {
1, 0, -1, 0};
int main()
{
int k, n;
scanf("%d%d", &k, &n);
c[1] = 1;
for (int i = 1, j = 1; i <= n / 2; i++, j += 2)
c[i + 1] = c[i] + 4 * (n - j);
for (int i = 1; i <= k; i++) {
int row, col, row2, col2;
scanf("%d%d", &row, &col);
row2 = row, col2 = col;
if (row2 > n / 2) row2 = n - row2 + 1;
if (col2 > n / 2) col2 = n - col2 + 1;
int t = row2 < col2 ? row2 : col2;
int x = t, y = t, start = c[t], x2 = 0;
while (start < c[t + 1]) {
if (x == row && y == col) break;
int nextx = x + dx[x2];
int nexty = y + dy[x2];
if (nextx >= t && nexty >= t && nextx <= n - t + 1 && nexty <= n - t + 1)
x = nextx, y = nexty, start++;
else
{
if (++x2 >= 4) x2 = 0;}
}
printf("%d\n", start);
}
return 0;
}
版权声明
本文为[Island blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212113548364.html
边栏推荐
猜你喜欢

Why is PostgreSQL about to surpass SQL Server?

返璞归真,多方安全计算要回归到“安全”的本源考虑

Product growth framework: from web2 to Web3

Tongda OA system docking single sign on platform use and Development Manual

剑指 Offer 15. 二进制中1的个数
![[ZigBee wireless communication module step by step] zigbee3 0 module to establish remote network control method](/img/d5/5102aae908f444e187ebc812e236a9.png)
[ZigBee wireless communication module step by step] zigbee3 0 module to establish remote network control method

工作流引擎 参数设置及业务引擎

The annual salary is 170W. Alibaba P8 blind date requires the woman's monthly salary of 10000. Netizen: it's a little high

其它——Redis与Mysql双写一致性方案解析
![[embedded] about IAP + XMODEM receiving bin file from outside to upgrade the chip](/img/05/c69e3701bf80f03c8ec35c033944b1.png)
[embedded] about IAP + XMODEM receiving bin file from outside to upgrade the chip
随机推荐
10.2 concentration
Module 3 operation architecture design document
AttributeError: ‘list‘ object has no attribute ‘endswich‘
迅为RK3568开发板交叉编译C程序
其它——Redis與Mysql雙寫一致性方案解析
32-bit and 64 bit computers
培训管理系统操作说明
【常用快捷键】
其它——Redis与Mysql双写一致性方案解析
Oracle management - tablespace permission control
通达oa工作流升级 操作说明
Swift 使用AVPlayer 和 AVPlayerItem 做语音播放
预处理问题
红日靶场--内网渗透练习
Sword finger offer 15 Number of 1 in binary
返璞归真,多方安全计算要回归到“安全”的本源考虑
Tips for using win10 close user tips before software installation
Others - interface test by postman
剑指 Offer 15. 二进制中1的个数
[报错]Content-Length vs downloaded bytes