AOJ_1031 Simple GUI Application
問題
解法
構文解析するだけ。
しなくてもできそう。
解析したあとは、再帰で一番上のパネルを求めることができる
#include <cstdio> #include <string> #include <vector> #include <algorithm> using namespace std; //タグ class T{ public: string name; int x1, y1, x2, y2; vector<T> ko; //子供 int kon; //子の数 T(int x1, int y1, int x2, int y2, string name) : x1(x1), y1(y1), x2(x2), y2(y2), name(name) { kon = 0; } }; char t_st[1010]; int N, p; string s_tug(); T tug_val(string); void e_tug(); void tug_struct(T &t){ //タグ構造 string name; if(t_st[p] == '<' && t_st[p + 1] != '/'){ p++; name = s_tug(); } T chi = tug_val(name); t.ko.push_back(chi); t.kon++; //printf("%s ==> %s\n", t.name.c_str(), t.ko[t.kon-1].name.c_str()); while(t_st[p] == '<' && t_st[p + 1] != '/'){ tug_struct(t.ko[t.kon - 1]); } e_tug(); return; } string s_tug(){ //開始タグ char str[1000]; int i; for(i = 0; t_st[p+i] != '>'; i++){ str[i] = t_st[p+i]; } str[i] = 0; p += i + 1; return string(str); } T tug_val(string name){ //タグ値 int x1, y1, x2, y2; sscanf(&t_st[p], "%d,%d,%d,%d", &x1,&y1,&x2,&y2); //printf("%s %d %d %d %d\n", name.c_str(), x1, y1, x2, y2); for(;t_st[p] != '<'; p++){;} return T(x1, y1, x2, y2, name); } void e_tug(){ //終了タグ for(; t_st[p] != '>'; p++){} p++; return; } T solve(T t, int x, int y){ T res = t; for(int i = 0; i < t.kon; i++){ if(t.ko[i].x1 <= x && x <= t.ko[i].x2 && t.ko[i].y1 <= y && y <= t.ko[i].y2){ res = solve(t.ko[i], x, y); break; } } return res; } int main(){ char c; while(scanf("%d\n", &N) && N){ scanf("%s", t_st); p = 0; T root = T(0, 0, 10000, 10000, "OUT OF MAIN PANEL"); tug_struct(root); for(int i = 0; i < N; i++){ int x, y; scanf("%d %d", &x, &y); T t = solve(root, x, y); printf("%s %d\n", t.name.c_str(), t.kon); } } }