d.cpp

来自「ACM大赛基本练习题」· C++ 代码 · 共 54 行

CPP
54
字号
#include<stdio.h>

const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, -1, 0, 1 };
const char dir[4] = { 'E', 'S', 'W', 'N' };
const char *dst[4] = { "forward", "back", "left", "right" };
const int f[4][4] = {
	{ 0, 2, 3, 1 },
	{ 1, 3, 0, 2 },
	{ 2, 0, 1, 3 },
	{ 3, 1, 2, 0 }};

int find_st( char st[] )
{
	int i;
	for( i = 0; i < 4; ++i )
		if( st[0] == dst[i][0] )
			return i;
	return -1;
}

void make_it()
{
	int n, i, len, turn;
	int nowx = 0, nowy = 0;
	int nowDir = 3;
	char st[10];


	scanf( "%d", &n );

	for( i = 0; i < n; ++i )
	{
		scanf( "%s %d", st, &len );
		turn = find_st( st );
		nowDir = f[nowDir][turn];
		nowx += dx[nowDir] * len;
		nowy += dy[nowDir] * len;
	}

	printf( "%d %d %c\n", nowx, nowy, dir[nowDir] );
}

int main()
{
	int dataCase, i;

	scanf( "%d", &dataCase );
	
	for( i = 0; i < dataCase; ++i )
		make_it();

	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?