2018训练题1

箱子

找规律按行输出

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int N;
		cin >> N;
		N *= 2; //后面频繁N*2 直接给N*2方便
		for (int i = 0; i < N + 2; i++) //第一行
			printf("-");
		printf("\n");
		for (int i = 0; i < N; i++) //中心
		{
			printf("|");
			for (int j = 0; j < N; j++)
				if (i == j)
					printf("\\");
				else if (i == N - j - 1)
					printf("/");
				else
					printf(" ");
			printf("|\n");
		}
		for (int i = 0; i < N + 2; i++) //最后一行
			printf("-");
		printf("\n\n");
	}
	
	return 0;
}

小刘的上古神器

主要思想就是 记录右箭头 左箭头和右箭头抵消
如果左箭头过多则肯定不行 如果串结束还有剩余的右箭头也不行
剩下的就是存储答案的技巧

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e4 + 10;
char s[MAXN]; //1维
int ans[MAXN];

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int N;
		cin >> N;
		int p = 0, k = 0; //最高能量 答案数量
		for (int i = 1; i <= N; i++) //编号从1
		{
			scanf("%s", s);
			int len = strlen(s), r = 0; //右箭头数量
			for (int j = 0; j < len; j++)
				if (s[j] == '>')
					r++;
				else
				{
					r--; //><<
					if (r < 0)
						break;
				}
			if (r == 0 && len >= p) 
			{
				if (len > p)
					p = len, k = 0;
				ans[k++] = i;
			}
		}
		if (k != 0)
			for (int i = 0; i < k; i++)
				printf("%d\n", ans[i]);
		else
			cout << -1 << endl;
	}

	return 0;
}

队列

#include <iostream>
using namespace std;

int q[10000];

int main()
{
	int x, l = 0, r = 0;
	while (cin >> x)
	{
		if (x != 0)
			q[r++] = x;
		else
		{
			if (l == r)
				cout << "NULL" << endl;
			else
				cout << q[l++] << endl;
		}
	}
	return 0;
}

the shy没那么牛皮

主要是BFS算法 可以百度学习一下

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 110;
char g[MAXN][MAXN];
bool vis[MAXN][MAXN];
int N, M, K;
int dir[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };

struct node
{
	int x, y, t; //t时间
};
int BFS(int x, int y)
{
	queue<node> q; //使用STL库queue更加方便
	q.push({ x, y });
	int t;
	while (!q.empty())
	{
		x = q.front().x, y = q.front().y, t = q.front().t;
		q.pop();
		if (vis[x][y]) //这里改为出队后再检测是否是回头路
			continue;
		vis[x][y] = 1;
		if (g[x][y] == 'T') //出队后再检测是否到达终点
			return t;
		for (int i = 0; i < 4; i++)
		{
			int xx = x + dir[i][0], yy = y + dir[i][1];
			if (xx >= 1 && xx <= N && yy >= 1 && yy <= N && g[xx][yy] != '#')
				q.push({ xx, yy, t + 1 });
		}
	}
	return -1;
}
int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		memset(vis, 0, sizeof(vis));
		cin >> N >> M >> K;
		int x, y;
		for (int i = 1; i <= N; i++)
		{
			scanf("%s", g[i] + 1);
			for (int j = 1; j <= N; j++)
				if (g[i][j] == 'S')
					x = i, y = j;
		}
		cout << BFS(x, y) << endl;
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/CaprYang/article/details/85215883
今日推荐