結果

問題 No.401 数字の渦巻き
ユーザー ramia777ramia777
提出日時 2022-05-20 22:20:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,466 bytes
コンパイル時間 884 ms
コンパイル使用メモリ 97,864 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 13:04:06
合計ジャッジ時間 2,667 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// yukicodr
// No.401 数字の渦巻き
//



//#include "stdafx.h"
#include <iostream>
#include <vector>
#include <list>//list
#include <set> //tree
#include <map> //連想配列
#include <unordered_set> //hash
#include <unordered_map> //hash
#include <algorithm>
#include <iomanip>

using namespace std;

#define RIGHT (1)
#define LEFT  (2)
#define UP    (3)
#define DOWN  (4)

vector <vector <int>> mass;

int N;

void solve(int i, int x, int y, int dir) 
{
	

	if (i > N * N)
		return;

	mass[y][x] = i;


	switch (dir)
	{
		case RIGHT:
			if (x + 1 < N && mass[y][x+1] == 0)
			{
				x++;
			}
			else
			{
				y++;
				dir = DOWN;
			}
			break;
		case LEFT:
			if (x - 1 >= 0 && mass[y][x - 1] == 0)
			{
				x--;
			}
			else
			{
				y--;
				dir = UP;
			}
			break;
		case UP:
			if (y - 1 >= 0 && mass[y-1][x] == 0)
			{
				y--;
			}
			else
			{
				x++;
				dir = RIGHT;
			}
			break;
		case DOWN:
			if (y + 1 < N && mass[y+1][x] == 0)
			{
				y++;
			}
			else
			{
				x--;
				dir = LEFT;
			}
			break;
	}

	
	

	solve(i+1,x,y,dir);
}



int main()
{
    cin >> N;
 	
	mass.resize(N);
	for (int i = 0; i < N; i++)
	{
		mass[i].resize(N);
	}

	for (int y = 0; y < N; y++)
	{
		for (int x = 0; x < N; x++)
		{
			mass[y][x] = 0;
		}
	}

	solve(1, 0, 0, RIGHT);

	for (int y = 0; y < N; y++)
	{
		for (int x = 0; x < N; x++)
		{
			cout << setfill('0') << setw(3) << right << mass[y][x]<<" ";
		}
		cout << endl;
	}
	
	//getchar();
	return 0;
}

0