oj地址:http://ac.jobdu.com/
第一次,我先介绍下这个oj吧,这个oj我在某神群内看到别人推广,所以就看了下。里面主要是历年计算机专业的机试题,中文居多。
A .考研海报
初看范围n,m都小于100,所以就直接二维数组暴力枚举。其实最优的方法应该是二维树状数组统计。偷懒没去写- -b
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
bool mat[105][105];
int main()
{
int n, m;
while(scanf("%d", &n) != EOF){
scanf("%d", &m);
memset(mat, 0, sizeof(mat));
int i, j;
while(m--){
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
if(a < 0)
a = 0;
if(b < 0)
b = 0;
if(c < 0)
c = 0;
if(d < 0)
d = 0;
for(i = a; i < c && i < n; ++i){
for(j = b; j < d && j < n; ++j){
mat[i][j] = 1;
}
}
}
int ans = 0;
for(i = 0; i < n; ++i){
for(j = 0; j < n; ++j){
if(mat[i][j] == 0){
++ans;
}
}
}
printf("%d\n", ans);
}
return 0;
}
B.占座位
模拟一下过程就好了。非常要注意下边界问题。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
int ss[105 * 105];
int use[105];
int main()
{
int n, m;
while(scanf("%d %d", &n, &m) != EOF){
int k;
char x[4];
int id, num;
int i, j;
memset(use, -1, sizeof(use));
memset(ss, -1, sizeof(ss));
scanf("%d",&k);
n = n * n;
while(k--){
getchar();
scanf("%s", x);
if(strcmp(x, "in") == 0){
scanf("%d %d", &id, &num);
if(use[id] == -1){
for(i = 1; i <= n; ++i){
if(ss[i] == -1){
for(j = i; j < i + num && i + num - 1 <= n; ++j){
if(ss[j] != -1){
break;
}
}
if(j == i + num){
use[id] = i;
fill(ss + i, ss + i + num, id);
puts("yes");
break;
}
}
}
if(use[id] == -1){
puts("no");
}
}
else{
puts("no");
}
}
else{
scanf("%d", &id);
if(use[id] == -1){
continue;
}
for(i = use[id]; ; ++i){
if(ss[i] != id)
break;
ss[i] = -1;
}
use[id] = -1;
}
}
}
return 0;
}
C.闯迷宫
入门BFS题目。用一个二维vis[x][y]数组记录在(x,y)时的最短时间。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
int mat[105][105];
int vis[105][105];
struct node{
int x, y;
int t;
}s, e;
queue<node> Q;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int n;
int bfs()
{
while(!Q.empty()){
Q.pop();
}
memset(vis, -1, sizeof(vis));
s.x = s.y = 0;
s.t = 0;
vis[s.x][s.y] = 0;
Q.push(s);
node first, next;
int i;
while(!Q.empty()){
first = Q.front();
Q.pop();
for(i = 0; i < 4; ++i){
next.x = first.x + dir[i][0];
next.y = first.y + dir[i][1];
if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < n && mat[next.x][next.y] == 0){
next.t = first.t + 1;
if(next.x == e.x && next.y == e.y){
return next.t;
}
if(vis[next.x][next.y] == -1|| vis[next.x][next.y] > next.t){
Q.push(next);
vis[next.x][next.y] = next.t;
}
}
}
}
return -1;
}
int main()
{
while(scanf("%d", &n) != EOF){
int i, j;
for(i = 0; i < n; ++i){
for(j = 0; j < n; ++j){
scanf("%d", &mat[i][j]);
}
}
e.x = e.y = n - 1;
if(n == 1){
printf("0\n");
continue;
}
if(mat[s.x][s.y] == 1 || mat[e.x][e.y] == 1){
printf("-1\n");
continue;
}
printf("%d\n",bfs());
}
return 0;
}