HDU6253-Knightmare【打表、规律】

    xiaoxiao2024-12-08  64

    A knight jumps around an infinite chessboard. The chessboard is an unexplored territory. In the spirit of explorers, whoever stands on a square for the first time claims the ownership of this square. The knight initially owns the square he stands, and jumps NN times before he gets bored.  Recall that a knight can jump in 8 directions. Each direction consists of two squares forward and then one squaure sidways. 

    After NN jumps, how many squares can possibly be claimed as territory of the knight? As NN can be really large, this becomes a nightmare to the knight who is not very good at math. Can you help to answer this question?

    Input

    The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case contains only one number NN, indicating how many times the knight jumps.  1≤T≤1051≤T≤105  0≤N≤1090≤N≤109

    Output

    For each test case, output one line containing “Case #x: y”, where xx is the test case number (starting from 1) and yy is the number of squares that can possibly be claimed by the knight.

    思路:打表找规律,附上打表代码和程序,注意会爆ll,所以要开ull。

    #include<set> #include<map> #include<queue> #include<cmath> #include<string> #include<cstring> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 1e4 + 10; int d[8][2] = {1, 2, 2, 1, -1, -2, -2, -1, -1, 2, -2, 1, 1, -2, 2, -1}; bool vis[maxn][maxn]; int n, cnt = 0; void dfs(int x, int y, int num) { if(num >= n) return; for(int i = 0; i <= 7; ++i) { int xx = x + d[i][0]; int yy = y + d[i][1]; if(xx >= 0 && xx < maxn && yy >= 0 && yy < maxn) { if(!vis[xx][yy]) ++cnt; vis[xx][yy] = true; dfs(xx, yy, num + 1); } } } int main() { // for(int i = 0; i <= 10; ++i) // { // cnt = 1; // memset(vis, false, sizeof(vis)); // n = i; // vis[1000][1000] = true; // dfs(1000, 1000, 0); // cout << cnt << endl; // } int t, t1 = 1; ull a[7] = {1, 9, 41, 109, 205}; scanf("%d", &t); while(t--) { ull n; cin >> n; printf("Case #%d: ", t1++); if(n < 5) cout << a[n] << endl; else cout << (14 * n * n - 6 * n + 5) << endl; } return 0; }

     

    最新回复(0)