T1 网络连接
- 高深的
sscanf
andsprintf
sscanf
是以字符串作为输入源
sscanf(str, "%d.%lld:%c||%lf", &a, &b, &c, &d);
这里的a是int, b是long long, c是char, d是double,他会返回所读取到的元素个数
同时自动忽略.:||
sprintf
则是送回字符串中
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <cstdio>
using namespace std;
int n;
unordered_map<string, int> h;
bool check(string ip){
int t[] = {0, -1, -1, -1, -1, -1};
int cnt = sscanf(ip.c_str(), "%d.%d.%d.%d:%d", &t[1], &t[2], &t[3], &t[4], &t[5]);
if (cnt != 5) return false;
for (int i = 1; i <= 4; i ++ )
if (t[i] < 0 || t[i] > 255)
return false;
if (t[5] < 0 || t[5] > 65535) return false;
char now[100];
sprintf(now, "%d.%d.%d.%d:%d", t[1], t[2], t[3], t[4], t[5]);
return now == ip;
}
int main(){
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ){
string op, ad;
cin >> op >> ad;
if (!check(ad)) puts("ERR");
else {
if (op == "Server"){
if (!h[ad]) puts("OK"), h[ad] = i;
else puts("FAIL");
} else {
if (!h[ad]) puts("FAIL");
else printf("%d\n", h[ad]);
}
}
}
return 0;
}
T3 Beingawesomeism
一般像这种大模拟思路简单,但是代码量极大
luogu的CF
其实样例给的很良心,所以我们从样例入手
- 其实我们可以发现,最多的步数就是 4 ,因为上下左右肯定就把这个矩阵覆盖掉了
步数为0:全是A
AAA
AAA
AAA
步数为1:有一条完整的棱
PPP
PPP
AAA
步数为2:角上有A或者中间有完整的边
APPP
PPPP
APAP
PPPP
或者
PPPP
AAAA
PPPP
PPPP
步数为3:棱上有A
PPPP
APPP
PPPP
PPPP
步数为4:中间有点
PPPP
PPAP
PAPP
PPPP
无解:根本没有A
PPPP
PPPP
PPPP
PPPP
理清思路后,代码如下
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 65;
int T, n, m;
char s[N][N];
bool allA;
bool find_len(){ // 有一条完整的棱
allA = true;
for (int j = 0; j < m; j ++ )
if (s[0][j] != 'A')
allA = false;
if (allA) return true;
allA = true;
for (int j = 0; j < m; j ++ )
if (s[n - 1][j] != 'A')
allA = false;
if (allA) return true;
allA = true;
for (int i = 0; i < n; i ++ )
if (s[i][0] != 'A')
allA = false;
if (allA) return true;
allA = true;
for (int i = 0; i < n; i ++ )
if (s[i][m - 1] != 'A')
allA = false;
if (allA) return true;
return false;
}
// 角上是否有A
bool angle(){return s[0][0] == 'A' || s[0][m - 1] == 'A' || s[n - 1][0] == 'A' || s[n - 1][m - 1] == 'A';}
bool find_ct(){ // 棱上有A
allA = false;
for (int j = 1; j < m - 1; j ++ )
if (s[0][j] == 'A')
allA = true;
if (allA) return 3;
allA = false;
for (int i = 1; i < n - 1; i ++ )
if (s[i][0] == 'A')
allA = true;
if (allA) return 3;
allA = false;
for (int i = 1; i < n - 1; i ++ )
if (s[i][m - 1] == 'A')
allA = true;
if (allA) return 3;
allA = false;
for (int j = 1; j < m - 1; j ++ )
if (s[n - 1][j] == 'A')
allA = true;
if (allA) return 3;
return false;
}
bool find_smlen(){ // 有完整的边
for (int i = 1; i < n - 1; i ++ ){
bool allA = true;
for (int j = 0; j < m; j ++ )
if (s[i][j] != 'A')
allA = false;
if (allA) return true;
}
for (int j = 1; j < m - 1; j ++ ){
bool allA = true;
for (int i = 0; i < n; i ++ )
if (s[i][j] != 'A')
allA = false;
if (allA) return true;
}
return false;
}
inline bool allisA(){ // 全是A
allA = true;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (s[i][j] != 'A')
allA = false;
return allA;
}
int step(){
if (allisA()) return 0;
if (find_len()) return 1;
if (find_smlen() || angle()) return 2;
if (find_ct()) return 3;
return 4; // 中间有点
}
int main(){
scanf("%d", &T);
while (T -- ){
scanf("%d%d", &n, &m);
bool havA = false;
for (int i = 0; i < n; i ++ ){
scanf("%s", s[i]);
if (havA) continue;
for (int j = 0; j < m; j ++ ) if (s[i][j] == 'A') havA = true;
}
if (!havA) puts("MORTAL");
else printf("%d\n", step());
}
return 0;
}