CSP-J2024T2
2025-07-04 16:59:15
发布于:广东
21阅读
0回复
0点赞
众所周知对于这种多样例的题目要进行初始化,正常我们会用全局数组+init。
欸,我有一计。不如我们直接使用指针,每次重新new一个对象,这样就不用初始化了。
于是用时40min便有了下面这篇代码。
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define fileO(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define fileC fclose(stdin); fclose(stdout);
#define pii pair<int, int>
using namespace std;
constexpr int MAXN = 1e3 + 9;
inline ll read(){
ll f=1,outt=0;char a=getchar();
while(a>'9'||a<'0'){if(a=='-')f=-1;a=getchar();}
while(a<='9'&&a>='0'){outt*=10;outt+=a-'0';a=getchar();}
return f*outt;
}
int k;
struct robot{
pii pos;
int face;
int ans;
};
struct mp{
int n, m;
char cmp[MAXN][MAXN];
bool bmp[MAXN][MAXN];
robot* cur_bot;
};
pii nxt_pos[4] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
auto robot_init()
{
auto bot = new robot;
bot -> pos = {read(), read()};
bot -> face = read();
bot -> ans = 1;
return bot;
}
auto mp_init()
{
auto new_mp = new mp;
new_mp -> n = read();
new_mp -> m = read();
k = read();
new_mp -> cur_bot = robot_init();
for(int i = 1; i <= new_mp -> n; i++)
{
for(int j = 1; j <= new_mp -> m; j++)
{
cin >> new_mp -> cmp[i][j];
new_mp -> bmp[i][j] = 0;
}
}
new_mp -> bmp[new_mp -> cur_bot -> pos.first][new_mp -> cur_bot -> pos.second] = 1;
return new_mp;
}
void turn_right(robot* bot)
{
bot -> face = (bot -> face + 1) % 4;
}
bool check(int x, int y, mp* now_mp)
{
if(x < 1 || x > now_mp -> n || y < 1 || y > now_mp -> m || now_mp -> cmp[x][y] == 'x') return 0;
else return 1;
}
void move(robot* bot, mp* now_mp, int nxt_x, int nxt_y)
{
if(!now_mp -> bmp[nxt_x][nxt_y]) bot -> ans++;// printf("%d %d\n", bot -> pos.first, bot -> pos.second);
bot -> pos = {nxt_x, nxt_y};
now_mp -> bmp[nxt_x][nxt_y] = 1;
}
void go(robot* bot, mp* now_mp)
{
auto pos = nxt_pos[bot -> face];
auto now_pos = bot -> pos;
if(check(now_pos.first + pos.first, now_pos.second + pos.second, now_mp))
{
move(bot, now_mp, now_pos.first + pos.first, now_pos.second + pos.second);
}
else
{
turn_right(bot);
}
}
void solve()
{
auto now_mp = mp_init();
auto now_bot = now_mp -> cur_bot;
// printf("%d %d\n", now_mp -> n, now_mp -> m);
// for(int i = 1; i <= now_mp -> n; i++)
// {
// for(int j = 1; j <= now_mp -> m; j++)
// {
// printf("%c ", now_mp -> cmp[i][j]);
// }
// cout << endl;
// }
for(int i = 0; i < k; i++)
{
go(now_bot, now_mp);
// printf("%d %d\n", now_bot -> pos.first, now_bot -> pos.second);
}
cout << now_bot -> ans << endl;
return;
}
int main()
{
// int T = 1;
int T = read();
while(T--)
{
solve();
}
return 0;
}
这里空空如也
有帮助,赞一个